GML Mouse Object Wall Collision

P

PandaFwiend37

Guest
I am working on a project where the player has to control an object using the mouse. I have the object being created at the mouse's location and statically stays following the mouse's location while moving. The issue I'm having is that I need it to have a collision with the walls of the level. I don't necessarily need the mouse to be confined within the walls, but at least the object needs to stay within the walls. Any ideas or help would be greatly appreciated!

I am using the following code within a controller object:

instance_create_layer(mouse_x,mouse_y,"Layer",oMouseObject);

and within the object I am creating:

x = mouse_x;
y = mouse_y;
 
Last edited by a moderator:

Bentley

Member
I am working on a project where the player has to control an object using the mouse. I have the object being created at the mouse's location and statically stays following the mouse's location while moving. The issue I'm having is that I need it to have a collision with the walls of the level. I don't necessarily need the mouse to be confined within the walls, but at least the object needs to stay within the walls. Any ideas or help would be greatly appreciated!

I am using the following code within a controller object:

instance_create_layer(mouse_x,mouse_y,"Layer",oMouseObject);

and within the object I am creating:

x = mouse_x;
y = mouse_y;
Do you want the player to stick (as you have it in your code) to the mouse, or to follow the mouse at a speed?

If you want the player to stick to the mouse, and you want the player to be stopped by walls, one idea is to collision check using lengthdir_x/y.

dir = point_direction(x, y, mouse_x, mouse_y);
dist = point_distance(x, y, mouse_x, mouse_y);
dx = lengthdir_x(dist, dir);
dy = lengthdir_y(dist, dir);
if (place_meeting(dx, dy, obj_wall))
{
// move to the wall using a loop
}
else
{
x = dx; // or mouse_x
y = dy;
}

I haven't made a game like that, so I'm sure others have better ideas.
 
P

PandaFwiend37

Guest
Do you want the player to stick (as you have it in your code) to the mouse, or to follow the mouse at a speed?

If you want the player to stick to the mouse, and you want the player to be stopped by walls, one idea is to collision check using lengthdir_x/y.

dir = point_direction(x, y, mouse_x, mouse_y);
dist = point_distance(x, y, mouse_x, mouse_y);
dx = lengthdir_x(dist, dir);
dy = lengthdir_y(dist, dir);
if (place_meeting(dx, dy, obj_wall))
{
// move to the wall using a loop
}
else
{
x = dx; // or mouse_x
y = dy;
}

I haven't made a game like that, so I'm sure others have better ideas.
Thanks for the reply! This is a start. Yes, I want it to stick to the mouse. I will check into this method and let you know my results!
 

Joe Ellis

Member
I have an idea
You could make the object follow the mouse but only move 1 pixel at a time, and use a repeat loop which will repeat for however many pixels (rounded) away the object is from the mouse,
that way, if the object gets blocked by a wall it'll stay rubbing against the wall until a clear route to the mouse is available

To optimise I would use about half the object's size for the amount moved per step, and divide the distance from the mouse by the this to give the amount of repetitions.

Another optimisation would be to use collision_line first and if there's no collision simply snap straight to the mouse
 
P

PandaFwiend37

Guest
I have an idea
You could make the object follow the mouse but only move 1 pixel at a time, and use a repeat loop which will repeat for however many pixels (rounded) away the object is from the mouse,
that way, if the object gets blocked by a wall it'll stay rubbing against the wall until a clear route to the mouse is available

To optimise I would use about half the object's size for the amount moved per step, and divide the distance from the mouse by the this to give the amount of repetitions.

Another optimisation would be to use collision_line first and if there's no collision simply snap straight to the mouse
I essentially got someone to help me through making code to do exactly what you described. Brilliant idea!
 
Top