

Then any position on the plane can be calculated via the equation: 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).Īlternatively 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. 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. 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: So (x,y) is the original vector, and (-y,x) is the vector normal to it. After the collision, the two objects stick together and move off at an angle to the -axis with speed. Lastly, if I loop through all vertices in A and B and let them collide with all segments of the respective other and repeat the same with B and A with the movement vector turned 180 degrees, the shortest distance any point can travel until a collision happens is the shortest distance A can travel until it collides with B. On the other hand, the second object, mass, initially moves at an angle to the -axis with speed.
#Vector 2d collision code
All code samples in this post are in TypeScript. In this case, the first object, mass, initially moves along the -axis with speed. I’ve been trying to learn how collision detection works this lead me to the Gilbert-Johnson-Keerthi (GJK) algorithm. Instead you want something simpler that calculates a normal vector to another vector. Figure 56 shows a 2-dimensional totally inelastic collision. 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.įirstly, if you are working on a 2D platformer you will not be needing the cross product which only exists in 3 dimensional space. I know the rest of the math, but I am having a hard time creating the normal vector that is required.

I am working on a game project and I am trying to perform vector reflection during collision. rather than just stretches of flat ground at varying angles. I am having a hard time finding a normal in 2D. 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.įor my game, I'm trying to create landscapes similar to what you would have seen in Sonic, curvy ground etc. Collision in 2 dimensions (head-on collision, no rotation, no friction) This is the simplest case where the direction of travel of both objects and the impact point are all along the same line: In this case the results are similar to the one dimensional case except that the velocities are expressed as two dimensional vectors. 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. Equations and Vocabulary for 2D Elastic Collisions Law of Conservation of Momentum: This law states that the momentum of a system, usually a collection of masses, remains constant unless an. but I'm obviously missing some important things, like defining a plane and obtaining the cross product. 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. I'm fairly new to game development (but not to programming) and I've seemingly forgotten everything but algebra from my highschool maths days.

Vector D = m_xDisplacement - xBody.m_xDisplacement ĭ = -(1.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. Here are two diagrams of what I'm talking about.Ĭan anyone point me into a right direction please?Īll information that can be given after the collision is the minimum penetration vector.ĮDIT: Found some code online that's very related to this but I don't understand it: void CBody::ProcessCollision(CBody& xBody, const Vector& N, float t) Currently I have a mini physics game which uses Separating Axis Theorem for collision detecting and response, however I came to a standstill when I discovered that there wasn't much documentation on what happens to an object's velocity after it collides with another shape using SAT collision detection.
