• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Mouse direction in 3D && camera direction

C

Clinton Wheeler

Guest
Hi All,

I am having an issue with the game I'm developing. I want the character to move towards the mouse pointer in 3D space, I am using the script I found on the GMC:

GML:
var _x = tde_aspect_ratio * dtan( tde_cam_fov /2 ) * (2 * window_mouse_get_x()     / window_get_width() - 1);
var _y =                    dtan( tde_cam_fov /2 ) * (1 - 2 * window_mouse_get_y() / window_get_height());

tde_mouse_vec[0] = _x * tde_proj_mat[0] + (-_y) * tde_proj_mat[1] + tde_proj_mat[2]
tde_mouse_vec[1] = _x * tde_proj_mat[4] + (-_y) * tde_proj_mat[5] + tde_proj_mat[6]
tde_mouse_vec[2] = _x * tde_proj_mat[8] + (-_y) * tde_proj_mat[9] + tde_proj_mat[10]
This works great, but only when my camera direction is at 90degrees. The game allows the user to rotate the camera direction with the middle mouse button and when I do this the character no longer moves towards the mouse (see preview below for example).

Screen capture GIF - Preview - Click here for example.

The camera direction is calculated like this:

GML:
tde_cam_x = global.player_object.x - lengthdir_x( tde_cam_in, tde_cam_dir (CAM DIRECTION) );
tde_cam_y = global.player_object.y - lengthdir_y( tde_cam_in, tde_cam_dir (CAM DIRECTION) );
And then the direction that the player object will face is done like this:
GML:
if not ( tde_view_dragging )
{
    z_rot = -(point_direction(0, 0, tde_mouse_vec[0], tde_mouse_vec[1]));
}
I have tried offsetting the point_direction with the actual camera direction but it doesn't seem to be always the same amount?

I am using GMS:2

If anyone can help I would greatly appreciate it

Regards,

GMWIZ
 

sp202

Member
Perhaps it would be easier to project to a 3D position at the mouse cursor and then perform a point_direction between that point and the character.
 
Top