Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411514 Posts in 69376 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 02:10:42 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Character walking "physics" on a heightmap
Pages: [1]
Print
Author Topic: Character walking "physics" on a heightmap  (Read 1711 times)
shrimp
Level 5
*****


View Profile WWW
« on: May 08, 2009, 12:57:31 PM »

Hello, I'm currently trying to get a character walking nicely on a heightmap.
The main problem I have is how to handle walking down slopes nicely. Currently (well, until I broke things Smiley) I had pretty decent results from snapping him to the ground on every frame, except when he jumps, when he gets an upward velocity and then resnaps onto the terrain once he lands.

HOWEVER, what I would like is for the player to be able to run off cliffs, and run down steep slopes with that kind of skipping motion that occurs when gravity isn't quite enough to pull him down to stay in contact with the slope. This is basically what you have in FPSs and stuff like Morrowind.

What happens when I have tried this is that I get horribly juddering stepped movement down slopes. So I was wondering - is there a standard approach to getting this movement on a slope working nicely? Any tutorials or pseudocode kicking around?

Cheers!

For context, the game looks pretty much like this:

(I'll probably start a devlog sometime, in case anyone's curious about the rest of it)
« Last Edit: May 12, 2009, 06:58:35 PM by Derek » Logged

Zaratustra
Level 7
**



View Profile WWW
« Reply #1 on: May 08, 2009, 01:23:22 PM »

- define a max slope where the player will walk down it instead of being subject to gravity, call it max_slope
- define gravity, call it gravity
- Calculate the ground height, call it ground_height
- Calculate the player height, call it player_height
- Calculate player vertical speed, call it player_vertical_speed
- Calculate if player is on the ground, call it player_ground

if (player_ground = true)
{
  if (ground_height < player_height - max_slope)
    player_height = ground_height;
  else
    player_ground = false;
}
else
{
   player_vertical_speed = player_vertical_speed - gravity;
   player_height = player_height + player_vertical_speed;
   if (player_height < ground_height)
      {
          player_height = ground_height;
          player_ground = true;
          player_vertical_speed = 0;
      }
}
Logged

shrimp
Level 5
*****


View Profile WWW
« Reply #2 on: May 08, 2009, 01:37:16 PM »

Hey, thanks! That looks reassuringly/embarassingly simple. I would have thought horizontal velocity would have to come into it somewhere, but maybe not! I'll post back when I've had a chance to try it out.
Logged

bateleur
Level 10
*****



View Profile
« Reply #3 on: May 08, 2009, 01:51:33 PM »

I would have thought horizontal velocity would have to come into it somewhere, but maybe not!

Zaratustra's formula uses ground height. For a sloping surface, the faster your horizontal velocity the faster the ground height will change. So it is a factor, just not explicitly.
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #4 on: May 08, 2009, 02:18:23 PM »

You'll discover that this problem gets more complicated the more you look at it. Zaratustra's post solves your problem perfectly, but after implementing it you'll notice you can walk up cliffs. Ok, so you use another max slope to prevent walking too steep. But then it looks ridiculous if you have a steadly increasing slope that it just suddenly switches to "wall". And so on. Tweaks and hacks abound, because walking is complicated. At some point, it's just better to use level design rather than a generic algorithm. I.e. you mark out your cliffs and slopes as such.
Logged
Aik
Level 6
*


View Profile
« Reply #5 on: May 08, 2009, 04:37:37 PM »

So - now that all the technical stuff is out of the way, I'd just like to say that your game looks awesome.
Logged
shrimp
Level 5
*****


View Profile WWW
« Reply #6 on: May 09, 2009, 12:18:38 AM »

Thanks again guys - still haven't got round to trying it but should manage later today

bateleur: Yep - I'm just surprised it was factored out of the final formula. My current code for this function is such a mess of experimental stuff, I wouldn't have spotted it  Smiley

Boris: Currently I have the character's horizontal speed modified by slope, so this is partially solved. I think it'll need reworking as it's too slippery at the moment. As for the "wall" problem, I think I'm going to have to solve that in a combination of ways: Marking slopes (procedurally - the world is PG), having a gradual transition from full-speed on flat to blocked on cliffs, and also having more of a state-machine for the character. We'll see!

Aik - thanks! Devlog plus test build containing this character movement to follow...
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic