# 2D Collision for Sidescroller

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

## Recommended Posts

Hi, I was wondering how I could make collision detection in a 2d- sidescroller. My terrain looks like This: I'm thinking of finding every point along the line between each vertex and testing it against my players feet. I know I need to use trigonometry, but seeing as I am in grade Ten and haven't had my math course yet, I don't really understand that yet. I also don't know how to make sure the whole foot is above the terrain, and not only the middle of the foot. Thanks! Adam

##### Share on other sites
You probably don't need to use trigonometry. If all of your objects are defined by polygons(which is what it looks like) I would recommend using line to line collision. Then if the player collides you can fix the collision easily by adding a vector perpendicular to the line collided with. It has been my experience that unless you are dealing with angles directly trig functions are rarely necessary(and actually slow things down). Unfortunately, vector math is a must and it is supposedly even more difficult(that's debatable).

A basic line line collision method would look something like this:

//a is points of first line//c is points of second line// p is the point that would be returnedlinecollide(double a1[], double a2[],double c1[], double c2[]){		m1 = (a1[1]-a2[1])/(a1[0]-a2[0]+0.000001) ;		m2 = (c1[1]-c2[1])/(c1[0]-c2[0]+0.000001) ;		b1 = a1[1]-m1*a1[0] ;		b2 = c1[1]-m2*c1[0] ;		p[0] = (b2-b1)/(m1-m2+0.000001) ;		p[1] = m1*p[0]+b1 ;	}

disclaimer: that method is conceptual it won't work as is, make sure to check that the collision point is actually on the lines afterward

##### Share on other sites
Could you possibly explain this in more depth, also showing me what you did in the code and formulas?

##### Share on other sites
It actually isn't important that the whole foot be above the line. Letting part of the foot sink into the hill in front of it can actually look descent, and possibly better than having the character balanced on the corner of its foot.

Is your terrain described by evenly spaced points? For each point that you want to be supported by the terrain (like the player's feet), do the following: Find the height of the terrain at the x coordinate of the point. To do this efficiently in your case, you would need to get the nearest terrain heights and linearly interpolate between them to get the height at that point. For example if the x coordinate was 27 and your heights were specified at intervals of 10 units, floor(27/10) = 20, so you would find the heights at 20 and 30. Then you would interpolate between them like (7/10)*(height(30) - height(20)) + height(20), to find the height at that point. Then you compare the point on your character to that height. If the character is below that height, you move them up to that height.

##### Share on other sites
Quote:
 Original post by AdamGLCould you possibly explain this in more depth, also showing me what you did in the code and formulas?
It looks like that code intersects two lines in slope-intercept form, but in general, you shouldn't use that approach in a game context. For intersecting linear components in 2d, it's better to use the implicit or parametric form. A google for '2d line intersection' should turn up some functions that do this.

As for the problem of keeping the whole foot outside the terrain, you may need to represent the player or player's feet with a bounding volume (such as a box) rather than just a line segment. What the best solution is depends on a few things though, such as whether your character is animated, or is always posed like in your screenshot.