Jump to content
  • Advertisement
Sign in to follow this  
tentoes

2d Vector based Collision

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I've started developing a 2d platformer and currently my collision system is based around axis-aligned rectangle bounds checking, which is working fine, however it's not enough for the purposes of this game. I'm fairly new to game development (but not to programming) and I've seemingly forgotten everything but algebra from my highschool maths days. I've been going through Vector maths tutorials and I am able to do things like getting a unit vector, getting a unit normal vector etc. but I'm obviously missing some important things, like defining a plane and obtaining the cross product. I'm also having a difficult time relating what I know to the concepts being talked about in the tutorials I've read so far. I'm hoping someone can point me in the direction of some tutorials which will help me get a better grasp on these concepts and also relate them back to collision detection. For my game, I'm trying to create landscapes similar to what you would have seen in Sonic, curvy ground etc. rather than just stretches of flat ground at varying angles. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately I don't have any good links to vector tutorials relating to collision detection, but I wanted to comment on a few points you brought up.

Firstly, if you are working on a 2D platformer you will not be needing the cross product which only exists in 3 dimensional space. Instead you want something simpler that calculates a normal vector to another vector. In 2 dimensions this is easy:

(x, y) -> (-y, x)

So (x,y) is the original vector, and (-y,x) is the vector normal to it.

Secondly, how do you define a plane in 2D space? Well there are many different ways that are valid, you can either define it using the 2D plane equation:

Ax + By = C

What does this equation mean? (A,B) is the normal vector to the plane you are defining, and C is the distance from the origin along the normal vector to the position of the plane. (x,y) is any point on the plane. So to define a plane all you need is the normal vector of the plane and the distance C from the origin (which can be calculated by dot producting any point on the plane with the normal).

Alternatively in 2D, a plane is simply an infinite line, in which case you can define it with 2 vectors, one is any position along the line P, the other is the direction of the line D. Then any position on the plane can be calculated via the equation:

(x,y) = P + Dt

where t is just some value that moves you forwards and backwards along the line.

I hope this helped in some way.

Share this post


Link to post
Share on other sites
The cross-product - or at least a version of it - can be useful in 2-D programming. In 2-D it's sometimes called the perp-dot product and is defined as if the (non-existant in 2-D) z-component of each vector is zero.

perpdot(a,b) = a.x*b.y - b.x*a.y

It tells you how perpendicular two vectors are. It'll return 0 for parallel or anti-parallel vectors, and a signed value otherwise.

I wrote a quick tutorial on vectors in 2-D programming, though I don't know how helpful it will be to you if you've read a few already. It's here. It still needs some work but it might help you.

[Edited by - shaolinspin on October 26, 2009 11:00:44 AM]

Share this post


Link to post
Share on other sites
Thanks for the replies and filling in the blanks in my knowledge.

I've found some tutorials which I think will be helpful for showing me how to apply my recently gained vector knowledge to collision detection problems.

I'll post any particularly helpful tutorials I find here for the people of the future.

Share this post


Link to post
Share on other sites
This is a pretty good vector and matrix math tutorial. Of particular interest for collision detection should be the section on projecting.

This is a good 2D collision detection tutorial. It's a little light on the math, though, so you'll have to figure out parts from the above tutorial.

Share this post


Link to post
Share on other sites
Hi, here I will post some source code, hope it make sence to you, the methods are form the book mathematics and physics for programmers

http://homepage.ntlworld.com/ricardo.sanchez/processing/collision_detection_1

Using those formulas you just need to set a wall that will act as the wall to bounce of, the code need some optimization but the basics are there

Cheers
rS

[Edited by - nardove on October 27, 2009 10:26:18 AM]

Share this post


Link to post
Share on other sites
Quote:

This is a pretty good vector and matrix math tutorial. Of particular interest for collision detection should be the section on projecting.

This is a good 2D collision detection tutorial. It's a little light on the math, though, so you'll have to figure out parts from the above tutorial.


thanks, i've actually seen both of those already, and i would say the first one is probably the best basic vector math tutorial (for beginners) i've come across, if there's anyone else reading this that needs to learn vector maths from the basics up.

and thanks for the code nardove, i've gone over it and it will be helpful to reference.

Share this post


Link to post
Share on other sites
Quote:

This is a pretty good vector and matrix math tutorial. Of particular interest for collision detection should be the section on projecting.

This is a good 2D collision detection tutorial. It's a little light on the math, though, so you'll have to figure out parts from the above tutorial.


thanks, i've actually seen both of those already, and i would say the first one is probably the best basic vector math tutorial (for beginners) i've come across, if there's anyone else reading this that needs to learn vector maths from the basics up.

and thanks for the code nardove, i've gone over it and it will be helpful to reference.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!