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

SOLVED Mouse in split screen

B

bobosen

Guest
Hey guys, so my project is coming along nicely thanks to you, it's a split screen shooter but i discovered something today.

Player1 uses mouse and keyboard, he can shoot 360 degrees around him BUT, anytime mouse hovers over player2 screen, the angle of Player1's gun changes to whatever sprite is shown on player2 screen. This is of course because the code is written to follow x and y coordinates.

Is it possible to limit mouse movement to viewport 0? I don't want to physically stop the mouse cursor at the split cause that's akward, I just want the program to not register the x and y coordinates of player 2 screen. Hope it makes sense, thanks

Here's the code from the gun shooting bullets (thanks Shaun Spalding)

GML:
/// @description Insert description here
// You can write your code in this editor
x = oPlayerAttack.x-0;
y = oPlayerAttack.y-10;

image_angle = point_direction(x,y,mouse_x,mouse_y);

firing_delay = firing_delay - 1;
recoil = max(0, recoil - 1);

if (mouse_check_button(mb_left)) && (firing_delay < 0)
{
    firing_delay = 5;
    recoil = 4;
    with (instance_create_layer(x,y,"Bullets",oBullet))
    {
        speed = 16;
        direction = other.image_angle + random_range(-2,2)
        image_angle = direction;
    }
    

}
 

rytan451

Member
Check if the mouse is within the first view.

GML:
var mx, my, vc, vw, vh;
mx = window_view_mouse_get_x(0);
my = window_view_mouse_get_y(0);
vc = view_camera[0];
vw = camera_get_view_width(vc);
vh = camera_get_view_height(vc);

if (point_in_rectangle(mx, my, 0, 0, vw, vh)) {
  // It's within the view.
}
 

Yal

🐧 *penguin noises*
GMC Elder
Mouse coordinates are relative to the room, not the screen... so if the second player is far away or in a different angle the mouse coordinates will reflect that. But there are functions to get screen-relative mouse coordinates which should make the angle behave more naturally (window_mouse_get_x / y iirc). You'll need to get the direction by comparing the player's screen coordinates to the mouse screen coordinates though. If the mouse player's view position is always in the top-left corner you can just subtract the view's corner coordinates from the player's room position to get that (camera_get_view_x / y iirc).

I might've gotten the function names wrong (they're so long UwU) so double-check with the manual before you start typing
 
B

bobosen

Guest
Thanks guys, interesting, I tried your code rytan451 and ran the if-code towards the angle of the gun, problem with this is, the moment I leave player1s screen with the mouse, the gun (ofc) stops rotating and is stuck in the last orientation before it left Player1s screen.

So thinking about it again, maybe my approach is wrong with this line of code

image_angle = point_direction(x,y,mouse_x,mouse_y);

Could I somehow bind the angle of the gun to follow the mouse position instead of it registering aim at physical x&y in the game room? That would solve the problem I think, just don't know how to code it. Thanks again
 

SoapSud39

Member
Utilize Yal's advice:
GML:
    //camera x/y (top left corner)
var cx, cy;
cx = camera_get_view_x(view_camera[0]);
cy = camera_get_view_y(view_camera[0]);

    //player x/y relative to camera x/y
var px, py;
px = x - cx;
py = y - cy;

    //mouse x/y relative to window
var mx, my;
mx = window_mouse_get_x();
my = window_mouse_get_y();

    //point direction
image_angle = point_direction(px, py, mx, my);
Just remember to add on whatever offsets you have. So, for instance, if your player 1 screen is (for whatever reason) to the right of the player 2 screen, add player 2 screen width to px. If player 1 is to the left or top or top-left of player 2, the code should work as-is (if I didn't mess it up).
 

Yal

🐧 *penguin noises*
GMC Elder
Thanks guys, interesting, I tried your code rytan451 and ran the if-code towards the angle of the gun, problem with this is, the moment I leave player1s screen with the mouse, the gun (ofc) stops rotating and is stuck in the last orientation before it left Player1s screen.

So thinking about it again, maybe my approach is wrong with this line of code

image_angle = point_direction(x,y,mouse_x,mouse_y);

Could I somehow bind the angle of the gun to follow the mouse position instead of it registering aim at physical x&y in the game room? That would solve the problem I think, just don't know how to code it. Thanks again
Already answered that 1 post ago:
Mouse coordinates are relative to the room, not the screen... so if the second player is far away or in a different angle the mouse coordinates will reflect that. But there are functions to get screen-relative mouse coordinates which should make the angle behave more naturally (window_mouse_get_x / y iirc). You'll need to get the direction by comparing the player's screen coordinates to the mouse screen coordinates though. If the mouse player's view position is always in the top-left corner you can just subtract the view's corner coordinates from the player's room position to get that (camera_get_view_x / y iirc).

I might've gotten the function names wrong (they're so long UwU) so double-check with the manual before you start typing
 
B

bobosen

Guest
Yal, sorry man, didnt mean to be arrogant, I just lack the understanding.

SoapSud39, thank you so much, for some obvious reason (not to me), the pivot point was stuck in the top left corner even though the camera was following the player. I added -400 to x and -300 to y and it works perfectly, many thanks to all
 
Top