
I noticed several people looking for Flashpunk physics from one of my earlier posts so I figured I'd share what I have with everyone. The class I've created will give you something similar to what Flixel has for physics although there are differences between the two and I'm not using any code from Flixel. So for those who are interested in some basic physics for Flashpunk then read on.
I suggest grabbing the source code from GitHub as it will contain the most up-to-date version of the physics class. It also contains a simple example to get you started. I will try to explain the Character class in detail below. But before we get started let's take a look at what the finished product is. All of the code for this example is in the GitHub repository.
Okay, now that you've seen what the finished product is let's take a look at the Character class. It's pretty basic and I think most programmers will be able to figure it out without my explanation but this is for those who don't do a lot of coding.
package com.matttuttle
{
import net.flashpunk.utils.*;
import com.matttuttle.PhysicsEntity;
// Example character class using simple physics
public class Character extends PhysicsEntity
{
// Constants for movement
private static const kMoveSpeed:uint = 5;
private static const kJumpForce:uint = 25;
public function Character(x:int, y:int)
{
this.x = x;
this.y = y;
// Set physics properties
gravity.y = 2.6;
maxVelocity.y = kJumpForce;
maxVelocity.x = kMoveSpeed * 2;
friction.x = friction.y = 1.6;
// Define input keys
Input.define("left", Key.A, Key.LEFT);
Input.define("right", Key.D, Key.RIGHT);
Input.define("jump", Key.W, Key.SPACE, Key.UP);
}
override public function update():void
{
acceleration.x = acceleration.y = 0;
if (Input.check("left"))
acceleration.x = -kMoveSpeed;
if (Input.check("right"))
acceleration.x = kMoveSpeed;
if (Input.pressed("jump") && onGround)
acceleration.y = -kJumpForce;
super.update();
}
}
}
First thing you should notice is that there are two constants at the top of the class, kMoveSpeed and kJumpForce. These constants will change the character's horizontal acceleration and jump height respectively. If you need the character to move faster or slower, change these two fields.
Next I want you to look at the physics properties section. It looks like this.
gravity.y = 2.6;
maxVelocity.y = kJumpForce;
maxVelocity.x = kMoveSpeed * 2;
friction.x = friction.y = 1.6;
The first line tells us that gravity is applied in a vertical direction (downward at 2.6). The next two lines set the maximum velocity that the player can move at. This makes sure the player doesn't keep acceleration faster and faster. The last line applies friction on the horizontal axis when the character is on the ground or vertically when on a wall. Increasing this value will help the character stop faster and decreasing it will give a sliding feeling.
Finally, the last bits of code setup our input keys (I used arrow keys and WASD). Then inside the update function we apply acceleration based on the keys that are pressed. One thing different between this and Flixel's physics is that gravity is applied on every frame so you don't have to do that in the update step.
If this has helped you and you're using this code in one of your projects please drop a comment below.