### Bad Avatar, No Physics for You!

Just when I thought I was making progress with my weekend project (a 3D adventure game) I find a serious flaw in my implementation: I cannot use the game engine's physics for the avatar.

Nothing is wrong with JME physics: They work perfectly well for e.g. ping pong, or vehicles in racing games. You assign a car a material and apply forces to it, and you'll get a life-like enough simulation of car behaviour on uneven ground. But cars are low and bulky. Avatars are tall and thin. And what happens if you apply a sideways force to a tall object? It staggers and topples over, naturally. It's not a bug, it's a feature! \*sigh...\*

So the problem is not (as I thought) that my avatar's physical properties are wrong. The problem is that I used physical properties for walking at all. Just think about it... When you walk, you don't just apply a sideways force to yourself. You shift your weight in a way that is so complex that you need to carry a whole extra CPU with you all the time (aka. cerebellum) just to calculate steps and balance... I think I pass on implementing a sense of balance for my avatars, thank you very much.

To give you a picture, the smooth kind of walking behavior that I want can be seen in this nice trailer for Spirit, a game that is currently being developed, using the same game engine (JME) as myself. Avatars in 3D games never seem to stagger, they walk like on rails, so what's the trick?

Well, drop physics, and code rails. Use `terrain.getHeight(x,z)` (which returns the terrain height (y) for any (x/z) position) at each step to set the avatar's (or cam's) y position to the terrain height + x, and ta-daa, smooth walking. In an advanced variation of that, you add extra "triangle picking" code to detect obstacles on top of the terrain (otherwise the avatar will not climb up on them).

As always with JME, it comes with a great sample for this method included, which you find at `jme/src/jmetest/intersection/TestObjectWalking.java`. I thought about combining it with a ThirdPersonController (`jme/src/jmetest/input/TestThirdPersonController.java`) but presently I just set the cam loc to somewhere close to the avatar.

With the sample code at hand, it's easy to implement. I hope to be able to use the triangle's "normal" value at the player's loc to determine whether the avatar is walking or climbing (so I can change the animation -- or even stop when the slope is too steep). A few downsides: By getting rid of physics and turning the avatar into a non-physical node, I also have to rewrite the code I already had for jumping. Second, I haven't yet figured out how to make the fancy triangle picker look inside a building too ("Look ma, I walk on the roof! Look ma, I -- fall through the floor!") And the third bug not yet dealt with: \*drumroll\*... the famous walk-through walls.

Hmmm. I wonder, if I didn't fix the walls, how long would it take the average player to notice they weren't real obstacles? ;-) Leave a comment, and tune in again next week!

Comments are closed for this entry.

NetBeans IDE, Java SE and ME, 3D Games, Linux, Mac, Cocoa, Prague, Linguistics.

##### Archives
Sun Mon Tue Wed Thu Fri Sat « July 2016 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Today
##### News

No bookmarks in folder