#### Archived

This topic is now archived and is closed to further replies.

# Accurate Collision

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m working on a small 2D platform side shooter game. I know how to deal with ''normal'' collision detection but my main problem is that after the player starts falling... he keeps on falling... then he reaches the floor. At this point the player should collide with the floor and stop. In order for the player to accurately collide with the floor without going a couple of pixels through I have to predict the player''s location. That''s not the problem though. The problem is when I need to predict collision when the player is moving both in the y axis and the x axis. When the player is about to collide, does he hit the floor, the wall and at which point exactly? For example: The player is 2 pixels away from the wall, and 6 pixels above from the ground moving at a speed that is: vX=3, vY=8... I somewhat have an idea of how to do this, but it seems WAY too long. In this case the player would hit the wall, but at which point? And is there a shorter way to do this? If anyone could just point me in the right direction. Input is greatly appreciated.

##### Share on other sites
After you''ve update the player''s position, do a check
if (Player.X >= Wall.X)
Player.X = Wall.X - 1;

if (Player.Y >= Floor.Y)
Player.Y = Floor.Y - 1;

That elimnates doing prediction, and just adjusts the player if he happens to go through the Wall/Floor.

Nutts

##### Share on other sites
also, the player should never be moving faster then your thickest wall during each update. if he is, then the previous technique wont qork, though in most 2d games this is a non issue (ie the player NEVER moves faster then the thinnest object).

also you check the players bound box, not a point.

also if you have an idea to do this with accuracy, but seems too long, dont be so lazy and test your theories out. you will never learn if you dont want to use an algorithm because its "way too long". look into rigid body physics simulations. they have VERY accurate collision detection. ignore the third axis and its all good. (dont be scraed by the math, much of it you wont be dealing with since you wont be calculation reflections and other physics, just concentrate on the discussion of the collision detection).

##### Share on other sites
Thanks, though

if (Player.X >= Wall.X)
Player.X = Wall.X - 1;

will only work if we''re dealing with the end of "the end of the level" wall.

I''m talking about walls [in the middle of the level] that are 1 pixel in width and about 30 in height.
The player sprite size is around 9x9.
I am not very familiar with the velocity ranges used in such a 2D game for the player sprite but if such a sprite happens to fall from the top of the screen, won''t its velocity on the Y-Axis be higher than 9 pixels per frame?

As for my long idea, I''d rather use a faster and more efficient way if possible.

I''ll experiment a bit but I''d like to hear some more opinions before I create the ultimate undefeatable bug.

Input, output and any other type of communication - greatly appreciated .

##### Share on other sites
Of course, if you''re desperate for a cheap hack that should fix the problem with falling through walls when your velocity is too high, try this: Check the player''s velocity, if it''s too high (in this case > 9 pixels an update), break the update into separate pieces and do it once for each piece. Something like this...

  bool CheckColl ( ... ){    if ( player.yvel > MAXVEL )    {        for ( int a = 0; a < ( player.yvel / MAXVEL ); a++ )            CheckColl ( ... );    }    else    {        // Do previously mentioned test.    }}

Some variation of this idea should work for you...

##### Share on other sites
I''d imagine there''s a two part solution to this:

1) Determine if the player went through the floor.

2) If so, calculate the intersection where the player hit.

Basically, for #2, you need to find the point along the movement line where you have a certain y value (the top of the floor). I would figure out one of the bottom points of your rectangle.

So your math problem is that you have a line that goes from (x0,y0) (the previous position of the point) to (x1, y1) (the location after the move, through the floor), and you want to solve for (x'',y'') such that y'' = the top of the floor.

I know there''s a simple equation to solve for x, but my high school math hasn''t been used for many years, so I''ll search around for the formula, and if I find it I''ll post it.

##### Share on other sites
Thanks for the support.
I know which equation to use in order to find the intersection point. It is a simple equation but it becomes a bit more complicated when the player''s movement line is about to intersect with a wall and a floor at once.
Anyways, I don''t think I''ll be needing that.
I''ll just keep the amount of frames per second high, thus the maximum velocity should remain smaller than the player''s width and height.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009714
• ### Who's Online (See full list)

There are no registered users currently online

×