Alright, this will dip into trig and some physics. I will include some shortcuts at the end. These are probably faster and easier, but you don't really know whats going on (great when it works, a living hell when it doesn't)
I apologize in advance if this gets really mathy, I will do my best to keep it simple.
Note that all labels in bold are vectors, if they aren't, then they are the magnitude of the vector.
First off, you want to stop the object from going through the obstacle. To do this, we will use vectors.
V1 will be your motion vector for the player/object.
V2 will be the vector that is normal to the surface being collided with (the obstacle). NOTE: Keep
V2 as a UNIT vector, that means it is only 1 unit in length. If you want an explanation of how to find
v2, I can give it (quickly: find the angle, find the normal, use trig to find the components).
First off, you perform the Dot product on the two vectors which project
V1 onto
V2 (which will show how big
V2 needs to be to stop
V1 from going through it). Here is what the math looks like:
abs(V1x*V2x+V1y*V2y)
This will give the magnitude of
V3 (the vector that represents V1 projected onto
V2).
Now for the physics.
The law of conservation of momentum states that in all collisions momentum is conserved.
Momentum is found by this formula:
P=M*V
Where P is Momentum, M is mass and V is the magnitude of the velocity.
So, because momentum is conserved, Pstart=Pfinal.
Pstart=M*V1
Pfinal=M*Vf
Vf is a new vector used to represent the final velocity vector of the object's motion. It can be expressed like so:
Vf=
V1-
V3+R
R is a variable to show that we will have to add something to make it work.
Since they are vectors, you can look at it like this:
Vf=<V1x-V3x+Rx,V2y-V3y+Ry>
So, to put it all together. We should have found
V3 by now and we have:
M*V1=M*Vf
V1=Vf
Note that V1=Vf is NOT the same as
V1=
Vf (the components will be different but the magnitudes will be the same).
Now the math gets....irritating, so here is the shorthand version:
R=
-V3
Meaning:
Vf=<V1x-2*V3x,V1y-2V3y>
Congrats you just solved science!!!
SHORTCUTS!!!!
These are solutions that SHOULD work, but I haven't tried. They will be shorter and easier to implement but probably harder to debug.
1. Reflection Matrix
|2cos^2(Phi-1) 2cos(phi)sin(phi)|
A=|2cos(phi)sin(phi) 2sin^2(phi)-1 |
Phi is the angle of the obstacle you hit.
I will spare you the math and give you the results.
These are just to make it easier on the eyes
Mtx1=2cos^2(Phi-1)
Mtx2=2cos(phi)sin(phi)
Mtx3=2cos(phi)sin(phi)
Mtx4=2sin^2(phi)-1
Vfx=Mtx1*V1x+Mtx2*V1y
Vfy=Mtx3*V1x+Mtx4*V1y
2. The angle between the obstacle and the V1 vector is the same as the angle between the obstacle and the Vf vector. Use some basic trig to figure things out. Can explain if necessary.
Can't think of anything else. Personally, I would just use the reflection matrix. However, those are much harder to debug than say doing the whole process at the beginning. Shortcut 2 is probably the simplest, but might give you some trouble here and there.
Hope I didn't lose you though all this!
Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose