Just a little clarification there:
And a short algebra session:
Code:
This is your character with the current 2D velocity vector shown:
O <- character
/
/ <- velocity
^ <- Normal of the ground
|
____|_____
GROUND
Note: a normal always points perpendicular to it's surface.
Then you add the velocity vector to the normal vector:
<<===O <- character/object
| /
| /
|/
To get out a vector of where your object SHOULD go..
That's this one: <<=====
But there is one trick, you must normalise the normal :)
In other words your normal must be of the same length as your velocity.
So first measure the length of the velocity:
Len = sqrt(sqr(Vel.X)+sqr(Vel.Y));
Then normalise the normal:
Len2 := sqrt(sqr(Norm.X)+sqr(Norm.Y));
if Len2 <> 0 then
begin
Norm.X := Norm.X / Len2;
Norm.Y := Norm.Y / Len2;
end;
Now your normal is guaranteed to have a length of exactly 1. No matter where it's
pointing.
Now just times it by Len to get it the same length as your velocity vector:
Norm.X := Norm.X * Len;
Norm.Y := Norm.Y * Len;
Then get the result vector by adding the two:
Result.X := Norm.X+Vel.X;
Result.Y := Norm.Y+Vel.Y;
Hope that helps.
Bookmarks