I try to change the getDesiredSpeed Method of the Character class, but I have hit a brick wall here after several hours:
Code:
Flt Chr::desiredSpeed() // get desired speed
{
// We try to calculate the height at the current position and
// The position 1 meter ahead in move direction. From these
// 2 Points we calculate a height difference, which we then
// use to increase or decrease the speed according to slope
Vec2 oldPos = Vec2(pos().x, pos().z);
Flt currHeight = Game::World.hmHeight(oldPos, true);
Vec2 offset = Vec2();
offset.rotate(angle.x);
offset.rotate(DegToRad(180.0f));
offset.setLength(1.0f);
//oldPos = Vec2(pos().x+1000.0f, pos().z+1000.0f);
Vec2 newPos = oldPos + offset;
//newPos = Vec2(newPos.x-1000.0f, newPos.y-1000.0f);
Flt newHeight = Game::World.hmHeight(newPos, true);
Flt diffHeight = (Flt) (currHeight - newHeight);
if (diffHeight > 1.0f) diffHeight = 1.0f;
else if (diffHeight < -1.0f) diffHeight = -1.0f;
Flt speed =(ctrl.flying() ? T.flying_speed : T.speed);
speed*=Lerp(0.33f, 1.0f, anim.walk_run); // slow down when walking
speed += diffHeight*(speed/2);
return speed;
}
What I try to do is the following: get the current Height, calculate a new position 1 meter ahead in the walking direction, calculate height there, get the height difference, cap it to a range (1 .. -1) and use it to modify the speed.... but:
It doesn't works right no matter how I try. It always seems that even if 3 direction work fine, one is off. And I really lost track of how the Esenthel Engine tracks its positions and angles. Especially as the pos seem to not start at 0, instead the point 0 is somewhere in the middle of the map.
anyone having a better idea how to solve this problem?
Also, my second problem is performance (noticing a huge FPS drop of sometime 10 Frames)... this method seems to be called every frame, even if the character is stationary. Where is it called? Is there a possibility to change the call to not trigger so often, or to move my own code to a different method where it only triggers when the character is moving?