vrijdag 8 oktober 2010

Hooray for physics and mathematics

To my dearest readers,

Unfortunately I've been mostly ill this week, but that doesn't mean I don't think about how to solve certain issues at work. For example I'm now adding slopes to the platform game I'm working on. One of the problems here is that when the character reaches a slope he speeds down and slides back. The desired behaviour however is that he moves on slopes just like he moves on the ground. Now as I've been asked to write a little bit more technical, I will be going into more details and describe to you how to solve this problem using high school physics (who knew it would actually come in handy one day?).


So consider the image below. Point S is the center of our character to which the force of gravity Fg is applied. Now to understand why we slow down and slide off the slope we change our frame of reference to that of the slope so that Fg is split up in two forces: force Fy which is perpendicular to the slope and force Fx which is parallel to the slope. So now we see why we slow down and slide back: force Fx pushes us off the slope. So to solve the problem we must apply a force Fc which is equal to Fx but exactly in the opposite direction so that it cancels force Fx. Note that we do not want to cancel force Fy since that's what will keep us on the slope.












So how do we find force Fx? This is where a lovely sine will help us out. For any triangle with a right angle (90 degrees) we can use that sin(angle) = length of opposite side / length of hypotenuse (see image). We can use this to find force Fx. Consider triangle (S, Fx, Fg) (yes, I'm abusing notation since Fx and Fg are vectors not points, but you get the point. No pun intended.), we apply our equation and get: sin(a) = d(S, Fx) / d(S, Fg) where function d returns the distance between two points. Remembering that Fx and Fg are actually vectors with origin S this simplifies to sin(a) = Fx / Fg. To find Fx we multiply the left and right hand side of the equation by Fg and get Fx = Fg * sin(a). Now we know Fg, but we don't yet know angle a.
 
 
 










To find angle a look at the image again. We will now find another triangle which is similar (has the same angles) to triangle (S, Fx, Fg), namely triangle (T, Fg, S). First notice that angle(S, Fx, Fg) (this is the angle defined at Fx, just like angle a can be described by angle(Fx, Fg, S)) is a right (90 degree) angle by construction because Fx is perpendicular to Fy. Then notice that angle(T, Fg, S) is also a right angle: the force of gravity is perpendicular to flat ground. Next we see that Angle(Fg, S, Fx) is the same as angle(Fg, S, T) because T is on the line defined by S and Fx. For any triangle we have that the sum of its angles is 180. So for triangle (S, Fx, Fg) we have that 90 + a + angle(Fg, s, Fx) = 180 and for triangle (T, Fg, S) we have that 90 + angle(Fg, S, Fx) + b = 180. From this we derive that a = b. And angle b is simply the angle of the slope.

So the force Fc we are looking for is now given by: Fc = -Fg * sin(angle of slope)
We apply force Fc whenever the force of gravity is applied and slopes will now be like walking on flat ground. This will work nicely until the designers of the game decide they also want slopes which the character cannot climb (they're evil like that), but that's a problem for another time.

Thanks for reading. I'm jumping back into bed and hope I'll be better tomorrow.

-- Stijn



Geen opmerkingen:

Een reactie posten