User Tools

Site Tools


en:guides:beginners:physics

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:

[Physics]
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:

[HeroObject]
Graphic      = HeroGraphic
Position     = (-350, 100, 0)
Scale        = 2
AnimationSet = HeroAnimationSet
Body         = HeroBody

Now to define the HeroBody section:

[HeroBody]
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:

[HeroBodyPart]
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.

[PlatformObject]
Graphic  = PlatformGraphic
Position = (-400, 270, 0)
Scale    = (54, 2, 0)
Repeat   = (27, 1, 0)
Body     = PlatformBody

Now the body and body part for a platform object:

[PlatformBody]
Dynamic  = false
PartList = PlatformBodyPart
 
[PlatformBodyPart]
Type  = box
Solid = true

Notice the slight difference from the HeroBody? 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:

[HeroBodyPart]
Type        = box
Solid       = true
SelfFlags   = hero
CheckMask   = platforms

The SelfFlags property is a string that describes the object and the CheckMask is a mask list of other object self flags it can collide with.

Now set the PlatformBodyPart flags:

[PlatformBodyPart]
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.

Our guy lands and stays on the floor. Good.

You can turn on physics debug mode if you want to see the bounding boxes around the objects. Turn on the physics debug with:

[Physics]
AllowSleep           = false
Gravity              = (0.0, 2400.0, 0.0)
ShowDebug            = true

You can see the boxes that surround the hero and platforms. That is because they have bodies.

Run that up and you should get the following:

Much better. Don't forget to turn off the physics debugging before continuing.


Next, Part 10 - Input Controls.

en/guides/beginners/physics.txt · Last modified: 2024/11/17 04:48 (6 weeks ago) by sausage