Slope Physics Tutorial II-Adapted from Builderboy’s method in Chainfire Pinball Library.
First, let us remind ourselves of a few basic vector operations…
Think of it as if you are following a path. These two vectors, when you add them, will take you to the same place that vector C would take you. Hence, they are equal.
Now, how do we add vectors? Easy; if you keep them in component form A
x+B
x= C
x and A
y+B
y=C
yNow, let’s take another look at our slope diagram. This time, let’s give the object some starting velocity. In this method of slope physics, we will only apply the physics if a collision is detected. In order to roll, the ball will have to collide with the slope every frame, and every frame the velocity will be modified appropriately.
In the diagram, the velocity is taking the object into the slope. We want to modify the velocity so that it is pointing in the right direction—we want the velocity to be transformed to become V
f. How can we do this? Note that adding the original velocity and the vector labeled N
f will give us V
f. So now all we need to do is find N
f…
Now we will use another type of vector math called the dot product. A dot B is defined as A
x* B
x+ A
y* B
y. The dot product also has another convenient property:
The || signs mean the magnitude of the vector; its linear length. Use the Pythagorean theorem to get it.
Now, more importantly, we can use this along with a little bit of trig to figure out what N
f is…If we move the normal vector over to where N
f is…
Remember that N
f and the original normal vector have the same direction. So, now that we have the magnitude of N
f, we can divide it by the magnitude of the original normal vector to get the ratio of their magnitudes. Then we can multiply that by their x and y components to get the components of N
f:
N
fx= N
x*| N
f|//|N|
N
fy= N
y*| N
f|//|N|
And now add these to our velocity components to get our final new velocity. Happy coding