Collision in Top Down

7

7bones

Guest
Hi.I have poblem with top down collision

Here is code in collision event

a = point_direction(x, y, other.x, other.y); //Get the direction from the colliding object
xoff = lengthdir_x(1, a); //Get the offset vector
yoff = lengthdir_y(1, a);

//Move out of collision
while (place_meeting(x, y, other))
{
x -=xoff //Move the instance out of collision
y -=yoff
x = round(x)
y = round(y)
}


Problem is big sliding.When player walk in something (like door,wall etc) slides in side to go out of collision- that looks very bad.

I also have collision system with some objects to Player cant move when walk into objects- but i also need that code to make all looks natural.

Here is question- what change in this code to make player stand exactly in same position while colliding- not slide anywhere??
 

CloseRange

Member
@7bones
I can't be 100% sure if I'm correct but better to at least try something.
The problem is you are more than likely never hitting the wall head on so you could be coming out of the collision in any manor.
I'm not home to test out my code but I'll give it my best shot:
Code:
a = point_direction(x, y, other.x, other.y);
xoff = lengthdir_x(1, a);
yoff = lengthdir_y(1, a);

x = round(x);
y = round(y);
while (place_meeting(x, y, other)) {
     if (xoff >= yoff) x -=xoff; else y -=yoff;
}
Also you only need to round the x and y once before the while statment because it'll continue to be whole (because you used 1 for the length)
 

NightFrost

Member
Sliding out of a collision is not the best of ways to do collision avoidance. Especially not in the way your code is doing it, testing against only one collider when in fact you might be in collision with multiple instances. Neither can you guarantee you are sliding back into original non-collision position if you just back away from the center of mass (assuming that's where x,y is), instead sliding into another collision you are not detecting.

It is better to do it in the way most (all?) collision tutorials do it. Establish a potential target position and check if there is a collision at that point. If there isn't, you can move there. If there is, step one pixel at a time on one axis at a time towards it until a collision would happen.
 
7

7bones

Guest
@CloseRange No diffrence, same innatural slide :/

Sliding out of a collision is not the best of ways to do collision avoidance. Especially not in the way your code is doing it, testing against only one collider when in fact you might be in collision with multiple instances. Neither can you guarantee you are sliding back into original non-collision position if you just back away from the center of mass (assuming that's where x,y is), instead sliding into another collision you are not detecting.

It is better to do it in the way most (all?) collision tutorials do it. Establish a potential target position and check if there is a collision at that point. If there isn't, you can move there. If there is, step one pixel at a time on one axis at a time towards it until a collision would happen.
I have working code for collision when walking.That code I need when player touch obstacles not from his choice -like being pushed on wall,kicked etc


edit.I forgot to write important thing - that is top down game-and also camera is rotating with player angle
 
Last edited by a moderator:
Top