User Tools

Site Tools


Part 9 – Physics

For our game we will need to add physics to keep our hero firmly on the ground.

The first thing to do is to enable physics in the config:

AllowSleep           = false
Gravity              = (0.0, 2400.0, 0.0)

This will set the strength and direction of the gravity and stops the physics being applied when objects settle. Our gravity will be pretty strong at 2400.

In order for physics to act on an object, the object needs to have a body. Let's add one to our hero:

Graphic      = HeroGraphic
Position     = (50, 400, 0)
Scale        = 2
AnimationSet = HeroAnimationSet
Body         = HeroBody

Now to define the HeroBody section:

Dynamic  = true
PartList = HeroBodyPart

Dynamic means that the object is affected by physics and gravity. Each Body must have at least one body part. Define that next:

Type  = box
Solid = true

The type is box and that means the collision area is a box shape surrounding the object. The part is also set to solid. That means that the object will not be able to pass through other solid body parts.

Run that and you'll see our hero fall right through the floor. So that's good. The physics body is working. But we need to get the platforms solid too.

Graphic  = PlatformGraphic
Position = (0, 570, 0)
Scale    = (54, 2, 0)
Repeat   = (27, 1, 0)
Body     = PlatformBody

Now the body and body part for a platform object:

Dynamic  = false
PartList = PlatformBodyPart
Type  = box
Solid = true

Notice the slight difference from the hero body. The PlatformBody is not dynamic, so it won't be affected by gravity. But the PlatformBodyPart is set to solid. This will allow the hero to collide with the platform without it falling away.

But the two won't affect each other until you set collision flags.

First, set the flags on the HeroBodyPart:

Type        = box
Solid       = true
SelfFlags   = hero
CheckMask   = platforms

The SelfFlags is a string that descibes the object and the CheckMask is a mask list of other object SelfFlags it can collide with.

Now set the PlatformBodyPart flags:

Type        = box
Solid       = true
SelfFlags   = platforms
CheckMask   = hero

So both objects are set to collide with each other.

Run the game and our hero will land and stay on the floor:

Next, Part 10 - Input Controls.

guides/beginners/physics.txt · Last modified: 2016/10/04 00:54 (6 months ago) by sausage