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

GMS2 - Collision for Mouse_x position not working

M

Morfic

Guest
Hi all. I am new to programming, and this program. I have an object that follows the mouse cursor position at a certain speed. That object can jump when I left click, and it obeys gravity and vertical collision.
However, for some reason the object walks right through horizontal walls. Is there any pointers you could lend me? Heres my code:


GML:
//Get Player Input

mouse_jump = mouse_check_button_pressed(mb_left);

//Calculate Movement

hsp = move_towards_point(mouse_x,y,4)
vsp = vsp + grv;
if (place_meeting(x,y+1,oWall)) && (mouse_jump)
{
    vsp = -7;
}

//Horizontal Collision

if (place_meeting(x+hsp,y,oWall))
{
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
        x = x + sign(hsp);  
    }
    hsp = 0;  
}
x = x + hsp;

//Vertical Collision

if (place_meeting(x,y+vsp,oWall))
{
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y + sign(vsp);  
    }
    vsp = 0;  
}
y = y + vsp;
 
Last edited by a moderator:

davawen

Member
Well, first, move_towards_point is a black box, if you look in the documentation, it returns N/A, and will move your object completely independently of whatever collision system you have, instead you want to calculate the speed yourself.

(btw you can get nice looking code with the code balise)
GML:
var hsp = sign(mouse_x - x)*wanted_speed;
//Also, in multiple places you do
a = a+7;
//But you can just use operators, it makes it easier to read
a += 7;
a -= 7;
a *= 7;
a /= 7;
 

Yal

šŸ§ *penguin noises*
GMC Elder
hsp = move_towards_point(mouse_x,y,4)
hsp will always be zero, since move_towards_point makes the object move using the built-in speeds and doesn't return anything. Thus your custom collision checking later doesn't do anything. And since your object is moved by the built-in speeds, the collision checking based on your custom horizontal speed variable will not actually do anything to stop it from entering walls.

Try this instead:
GML:
hsp = sign(mouse_x - x)*4
 
M

Morfic

Guest
I have replaced those pieces of code with operators and added my snippet to a code block.

I have also tested the suggested change, and it works like a charm. It also fixed my issue with my character swapping to face left or right. Thanks guys!

The only thing I am noticing now, is that whenever my mouse pointer hovers in one spot, the character moves to the pointer and then vibrates/swaps back and forth really fast on top of the pointer. Is that a problem with my code, or just with my logic? I am researching now a method to set speed to 0, whenever the character collides with mouse_x.

Code:
//Get Player Input
mouse_jump = mouse_check_button_pressed(mb_left);

//Calculate Movement
hsp = sign(mouse_x - x)*4
vsp = vsp + grv;
if (place_meeting(x,y+1,oWall)) && (mouse_jump)
{
    vsp = -7;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,oWall))
{
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
        x += sign(hsp);  
    }
    hsp = 0;  
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,oWall))
{
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
        y += sign(vsp);  
    }
    vsp = 0;  
}
y += vsp;


//Animation

if (!place_meeting(x,y+1,oWall))
{
    sprite_index = sCat;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = sCat;
    }
    else
    {
        sprite_index = sCatR;
    }
}

if (mouse_x != 0) image_xscale= sign(mouse_x - x);
EDIT: I have tested adding the following:

Code:
if (place_meeting(x,y,mouse_x)) hsp = 0

But sadly it did not have the effect I was looking for. It seems to erratically work now. Sometimes the character will stop vibrating. And sometimes it will disappear all together now. Or just vibrate as normal. hah
 
Last edited by a moderator:
Try something like
Code:
if (point_distance(mouse_x,y,x,y) <= hsp) { // If the horizontal distance is less than the speed we are moving horizontally
   hsp = 0; // Set hsp to 0
}
Put it just before this line, so that it effectively cancels the hsp before it is applied to the x and yet after the hsp has been calculated for movement:
Code:
x += hsp;
 
Top