• 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!

GameMaker Mouse x and y not correct when using matrix projection

Hello everyone,

I imported this little engine that allows me to import Blender models into the game. However, after activated, my character will no longer look at the mouse, but at a specific point in the room.

The character looks around with: point_direction(x,y,mouse_x,mouse_y)

The weird thing is, mouse_x andy won't change by moving the mouse (or rather, the only change by only 1 when moving it across the screen).

However, if start moving the camera around then the numbers will change rapidly

The lines of code that make the mouse do this are:

gpu_set_cullmode( cull_counterclockwise );
projMat = matrix_build_projection_perspective_fov( 60, (global.default_camera_w)/global.default_camera_h, 32, 32000 );
camera_set_proj_mat( view_camera[0], projMat );
camera_set_update_script( view_camera[0],
camera_update_script );

and this is for every step

mLookat = matrix_build_lookat(obj_camera.x,obj_camera.y,1200,obj_camera.x,obj_camera.y-7,190,0,0,1);
camera_set_view_mat(view_camera[0], mLookat);


I hope some of you can help me fix this.

thanks :)
 

Bart

WiseBart
mouse_x and mouse_y are transformed by the projection matrix as well.
I'm not sure what the values end up being, but I'd expect something like the intersection point between the line drawn from the camera position through the point on the near plane that is at (mouse_x, mouse_y) and the plane at z = 0 (or depth = 0). I haven't verified this, though...
Also notice, in your current code, that you're not using the variable direction in any of the calculations for the parameters to matrix_build_lookat. That way, you can't expect the outcome to be right.

When working in 3D, you can get the mouse coordinates in the window using window_mouse_get_x and window_mouse_get_y. The window coordinates aren't transformed by any of the matrices.
A 3D mouse camera is usually done by centering the mouse in the window, then adding a multiple of the amount it moves in the x and y direction in the previous step, then re-centering the mouse, and so on. You then add that amount to the current horizontal and vertical view angles.
Something like this:
Code:
// << Create Event >>

zdir = 0;    // Add an angle for up-down movement of the camera
z = 1200;

// << Step Event >>

// Get window dimensions
var ww = window_get_width();
var wh = window_get_height();

// Mouse input
mouse_xdiff = window_mouse_get_x() - ww/2;
mouse_ydiff = window_mouse_get_y() - wh/2;
mouse_xdiff /= ww;
mouse_ydiff /= wh;

direction += xdiff;    // Horizontal movement of the mouse means look left or right
zdir += ydiff;         // Vertical movement of the mouse means look up or down

// Re-center the mouse
window_mouse_set(ww/2, wh/2);    // Reset the mouse so we can get a new offset from the window center next step/frame

// Update view matrix
view_mat = matrix_build_lookat(x,y,z,x+lengthdir_x(1,direction),y+lengthdir_y(1,direction),z+lengthdir_y(1,zdir),0,0,1);
 
I think you have misunderstood, the game is not totally 3d, it is a topdown game with 3d elements :)

I tried windows_mouse_get_x/y but it still won't look in the right direction :/
 
Last edited:

Bart

WiseBart
Oh okay. I didn't get that from your initial post :)
In that case, what does it mean for the character to 'look around', since the game is topdown.

So you're using windows_mouse_get_x/y now but where do you use those values to change the camera projection?
Can you post the code you currently have?
 
The player moves the mouse and the character rotates to face it. The camera projection doesn't change because the camera stays the same (Think GTA2)

I fixed it by using
direction = point_direction(window_get_width()/2, window_get_height()/2, window_mouse_get_x(), window_mouse_get_y());

However, this only works when the player is in the center of the screen. In two players, the center of the screen is not always on the player, and when that happens it starts doing funny things.

I am now working on that, however any help is appreciated :)
 
Last edited:
R

rantedd

Guest
Hope somebody helps you, back when I was starting out with programming I would throw the projects away super fast if I wasnt able to find a solution myself. It was pure chaos time for me because I would get upset quite fast but eventually I got to the point where I just realized its way more efficient to seek help online and use other peoples knowledge :D
 
Top