次の方法で共有


Physically based Characters!

Hello…World!

First, you can see more articles like this at my website:  www.IndieDevSpot.com

So this just gave me a good run for my money for a while. For the longest time, I’ve been building all of my Characters completely wrong and the bug didn’t really surface until this latest game. Lets start by talking about what a Physically based Character even is.

physics character

What is a physically based character

1. A game object which is controlled by a player with input.

2. Reacts to the physics system in conjunction with player input.

 

So what was I doing wrong before?

Almost every tutorial you will ever find tells you how to build a 2D Character pretty much wrong, and this is what the movement code probably looks like:

1 2 3 Vector2 newVelocity = this.cachedBody.velocity; newVelocity.x += xSpeed * this.maxSpeed; this.cachedBody.velocity = newVelocity;

Lets talk about why this code won’t work for a physically based character.  Because it resets the velocity every frame!  So if my character gets a force added to it from an explosion or is sliding down a hill, the x component of the velocity using this strategy will be reset every frame to the input.

So how do I do it right?

Doing it right is exactly the same number of lines.

1 2 3 Vector2 pos = this.cachedBody.position; pos.x += xSpeed; this.cachedBody.position = pos;

So in this, we are just adding to the position the input quantity, xSpeed.  This allows the physics system to have complete control over velocity, we don’t touch it at all, and we move the rigidbody’s actual position giving us an “addative” effect ontop of the physics system, which is what we want.  Now when I shoot Mr. Jack Round out of the cannon, he flies out, interacts with the world physically while I still am able to give my players input control over him while aligning with the physics system.

This is how I will be moving almost all of my characters from here on out.

To check out more of my articles.  Check out my blog: www.IndieDevSpot.com

Comments

  • Anonymous
    September 22, 2014
    Hi, How can the same be done with XNA? Thanks! visualstudio.uservoice.com/.../3725445-xna-5

  • Anonymous
    November 10, 2014
    Hello, You have to track collisions, physics and movement all separately in various phases.  Its a bit complex honestly and that's why I'm glad to have Unity3D now :).  I move the rigidbody's position, because when you move the rigidbody, it moves, detects collision and moves back in the same frame.  You would have to repeat that with XNA. Thanks, ~David