Collision when moving toward mouse (no mask rotation)

D

Darren

Guest
How do I figure collision when I move my object based on mouse movement? I'm not physically rotating the mask, I'm using draw sprite trickery, but the movement going in a direction according to the mouse is screwing up any collision code I attempt. Basically I need my character to always move toward the cursor, which it does, but I can't figure out walls.

This is my code with no collision

dir = point_direction(x, y, mouse_x, mouse_y);
if distance_to_point(mouse_x, mouse_y) > 1{
{
if UpKey
{
x+=lengthdir_x(vspd,dir);
y+=lengthdir_y(vspd,dir);
}

if LeftKey
{
x += lengthdir_x(hspd ,dir+90);
y += lengthdir_y(hspd ,dir+90);
}

if RightKey
{
x-=lengthdir_x(hspd ,dir+90);
y-=lengthdir_y(hspd ,dir+90);
}
}}

if DownKey
{
x-=lengthdir_x(vspd,dir);
y-=lengthdir_y(vspd,dir);
}

I can't add collision how I would a normal top down game where you are always "W" for up, because sometimes "W" becomes down, left or right depending on where the mouse is. How would I go about sorting this?
 
Last edited by a moderator:

b∩bo

Member
If I understand your problem correctly, you are indeed moving the mask if you are altering the x and y, so make sure the object has the sprite assigned even if you are drawing it from the draw event.
 
D

Darren

Guest
If I understand your problem correctly, you are indeed moving the mask if you are altering the x and y, so make sure the object has the sprite assigned even if you are drawing it from the draw event.
Sorry, what I mean is that frequently people use image_angle to rotate their sprite, using this causes your mask to rotate. I am not rotating the mask, I'm simply rotating the sprite giving the illusion that my object is turning. The mask does indeed move with the player, you are correct, I just mean it isn't rotating. (Collision with true rotation is a lot trickier, this should be relatively simple in comparison!)
 

b∩bo

Member
Sorry, what I mean is that frequently people use image_angle to rotate their sprite, using this causes your mask to rotate. I am not rotating the mask, I'm simply rotating the sprite giving the illusion that my object is turning. The mask does indeed move with the player, you are correct, I just mean it isn't rotating. (Collision with true rotation is a lot trickier, this should be relatively simple in comparison!)
You can always change image_angle along with your other variable, it wont be visible but the mask should rotate.
 
D

Darren

Guest
You can always change image_angle along with your other variable, it wont be visible but the mask should rotate.
I know, I don't want the mask to rotate! I just want a collision system that works with my movement, the only reason I bring up rotation is that it should be easier to solve due to the fact I don't need rotation.

The confusion comes from the fact that usually I'd check for place meeting at for example y - 5 if I was pushing the up key, but now the up key can move in any number of directions based on where my cursor is, and it's confusing me how I go about writing a collision system for this movement.
 

b∩bo

Member
I know, I don't want the mask to rotate! I just want a collision system that works with my movement, the only reason I bring up rotation is that it should be easier to solve due to the fact I don't need rotation.

The confusion comes from the fact that usually I'd check for place meeting at for example y - 5 if I was pushing the up key, but now the up key can move in any number of directions based on where my cursor is, and it's confusing me how I go about writing a collision system for this movement.
Oh ok my bad. Well there are multiple ways of going about doing this.
One way would be instead of checking for place meeting at y - 5 you check place meeting at
x+=lengthdir_x(vspd,dir);
y+=lengthdir_y(vspd,dir);
 
D

Darren

Guest
Fixed it myself with such a simple solution. Some time away from the PC was all I needed!

This is how I did it

if UpKey
{
if !place_meeting(x,y -5,oWall) && mouse_y <= oPlayer.y
|| !place_meeting(x - 5,y,oWall) && mouse_x <= oPlayer.x
|| !place_meeting(x + 5,y,oWall) && mouse_x >= oPlayer.x
|| !place_meeting(x,y+5,oWall) && mouse_y >= oPlayer.y
{
x+=lengthdir_x(vspd,dir);
y+=lengthdir_y(vspd,dir);
}
}
 
D

Darren

Guest
Oh, and thanks for your help by the way, I do appreciate people taking the time to help me. :)
 
Top