`

Making Physics Based Side-Scroller Part 2: Adding animated character that moves

 
阅读更多

http://overlap2d.com/making-physics-based-side-scroller-part-2-adding-animated-character-that-moves/

 

 

Screenshot 2014-10-18 12.54.15

 

Platformer with a Player

Good morning everyone, and as you probably guess in this tutorial we are going to continue where we left before in developing our physics based small platformer.  Assuming you have already gone though Part 1 of the tutorial, you have a working project with an overlap2d scene loaded and proper physics shapes set to the platforms. Next step  as you see from the picture above is to add a playable character!

In this tutorial we are going to:

  • Import a sprite animation
  • Re-group our scene a bit
  • Pseudo Physics
  • Create a PlayerController script
    • Implement player moving
    • Implement player jumping
  • Create fake gravity
  • Do some ray-casting for falling collision

Importing Sprite Animation

I am using a sprite animation of a sheep, that you can download here (Or you can use your own), it is basically several png images with same name and a number of frame at the end. You can import them as a sprite animation directly to the oevrlap2d, just go to import dialog, and then choose sprite animations (make sure not to mistake with spine animations). Then locate animation images and select them all together, and then import. A nice Cute sheep will appear in the right middle panel under other assets.

Put it on the stage. Then right Click on it and choose convert to composite item. This is required because we are planning to attach script to the player, and you can only attach a script to a composite item. Besides later we may put other components inside player to combine with animation. When you click on the animation and it get’s selected in it’s bound box, you can see that animation is slightly off from the bottom, legs are a bit up. Assuming we do not want sheep to be floating in the air, lets get inside the composite item we just created by double clicking on it, and lower the sheep animation few pixels down so legs will touch the bottom bound. While you are inside the composite it is good idea to give animation an ID for later use. this is done from the basic properties panel on top right, choose a unique id like “animation”. Then get one level up from the composite item to the root scene by double clicking on an empty area.

Lastly you need to give an id to the main player composite item as well. I decided to go with “player”

Save your scene, and then export it by clicking Ctrl+E

Re-grouping the scene

In this tutorial we will be handling simple movements like left and right and jumping. There will be no movements on angles, so please remove all the platforms on the scene that are rotated, and leave only horizontal ones, as well as the ground platform, save and export when done. Also remove any dynamic bodies from previous tutorial, we won’t be needing them today. If you run the application now you should see the sheep walking in the air.

Pseudo Physics

There are several approaches on how to move a character inside the platformer. The most simple one is make it a dynamic physics body, and then just apply different forces to it to move. This is good when you need a complex things like you see in Limbo, but you also will have to spend a lot of time in tweaking your physics configuration. On the other hand in games like Super Mario Bros, you can see that character behaves in rather unrealistic ways like stops instantly like there is no inertia and things like that. In this tutorial we are going to explore the second option. To achieve that type of effect, you do not need physics, it should be just a simple code, very light math and collision detection with the world. Luckily for us, we already have a physics world with shapes set up, so we do not need to worry about doing collisions, as we have a ray-casting for that matter. Ray casting is a technique to detect nearby body fixtures. you specify the vector of your ray (point from and point to) and if that vector intersects any of the world fixtures you will get the intersection points. This way if we cast a ray from the bottom of the player we would be able to detect platforms under the character and stop the falling process. Sounds simple? Let’s get to it.

Creating PlayerController script

Our sheep will be controlled by an IScript that we are going to attach to our “player” composite item. This class will handle everything from starting stopping animations, handling input events, simulating pseudo physics and so on. Go ahead and create that class, and implement IScript interface from the runtime. So you would be able to attach that script to a composite item later.

The init method is getting composite item in effect, and the act method is called every frame. Make sure to write a constructor that gets GameStage reference and stores it so we can use it later to get the world and do some ray-casting:

In the init method store the item for later access from the act.  Besides that inside the init method we can retrieve our animation by id we gave earlier, pause the animation by default and set the origin coordinates for our player for better rotation/scaling.

Perfect, Go ahead and attach that script to the player composite item from our GameStage like this:

Now when you run the application it should compile normally and sheep should not move as the animation is paused. Next step is to listen for key input and move sheep left or right. We do not need any fancy speed calculation techniques here, to have acceleration or deceleration or things like that. It’ls a lot more simpler if A or D keys are pressed we move with constant speed, and if not then player just stays as is. You can either add input listener to the stage or just in act method of player script check for input, as libGDX provides us with that. Here is how:

In this code snipped we also scale X to -1 when moving left so the sheep will be looking in the direction it walks to. And we start and stop animation based on our knowledge if player walks or not.

Create fake gravity

You can run the game and move player left or right. Next step is to add pseudo gravity so sheep wall fall down, and jumping so sheep wall jump up! You will need to have a variable to keep current vertical speed, and change it on each frame according to gravity value (not necessarily the real gravity of our set up physics world). When jumped (space pressed) we will immediately make out vertical velocity to be a high value, and then on each frame (act) change our Y coordinate by value of vertical velocity.

Also add this class variables

You can try this out and jump around, fall and go up, should be fun, but no platform collisions yet, so let’s get to it.

Ray Casting and Collision with ground

First of all let’s implement a method that does the ray-casting, and if a collision is there stops the player from falling further.

just call that method every time in act before we move the object by Y coordinate, and now player should fall firmly on the ground and not fall through it.

Final Result

What is remaining is to make some coding to prevent jumping in the air so jump only on the ground, here is the final PlayerController.java class

Run the application, jump around and have fun! Compose your level so it will have platforms put in a way so you can reach unreachable areas and so on.

What can be done better?

I was curious on what you guys think could be done better, as I am too new to the box2d thing. So any suggestions are a lot welcome. Here are my highlights/questions:

  • We cast just one ray from the middle, meaning player should stay on platform with it’s middle part, which is bad, should we cast 3 rays instead?
  • The Box2D seems to be not giving immediate result for ray casting but provides handler, that means that theoretically we can go through object and then come back. Any better solution?
  • I was thinking how to move on angled objects, it seems that the best idea is to cast ray from the middle of player by Y coordinate, so it will not get under the object when moving left/right
  • For some reason sometimes randomly box2d crashes without an error, do you guys have it? any ideas on how to do it better?

Please leave all your comments/questinos/suggestions in questions in the forums here:  http://overlap2d.com/forums/topic/making-physics-based-side-scroller-tutorial-series/

If you find a bug here, let me know too!

Have fun coding this, and make something cool please :))

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics