How can I move dynamic object? (SOLVED)

I am making a player that can move but after I change the collision type from Kinematic to Dynamic, it can’t move by change the position.

I try disable the collision then move then enable the collision.

function init(self)
    self.vel = vmath.vector3()
end

function update(self, dt)
    msg.post("#collisionobject", "disabled")
    local pos = go.get_position()
    pos = pos + self.vel * dt
    go.set_position(pos)
    msg.post("#collisionobject", "disabled")
end

After I test, the player can move. But when it fall, it fall slowly.
How can I make it fall faster?

Usually you would want to keep it kinematic, why do you need dynamic?

I want the block can hit the player like mario.

The usual way to move a dynamic body is with the “apply_force” message. Kinematic bodies are often easier to make characters with, but it depends on the project. You can check the physics manual for an example of how to handle kinematic collisions.

You can change the gravity acceleration in your game.project file, or at runtime with physics.set_gravity().

If you’re making a platformer, you may want to check out Platypus.

3 Likes

Indeed, as @ross.grams said, by changing gravity either in game.project or in some function.

You are disabling the same #collisionobject twice in one function (update). Is it for purpose?

You probably want to disable collision object of type Kinematic, e.g.

msg.post("#collisionobject_kinematic", "disabled")

and enable collision object of type Dynamic, e.g.

msg.post("#collisionobject_dynamic", "enabled")

when you want the game object to behave dynamically, which means, all collisions, rotation and position handling will be performed by Box2D (physics engine), without your intervention (except calling apply_force). But I would suggest switching collision objects only in the particular case, when you need it and after the case is finished, switching it back to kinematic. Or create only dynamic collision object and change position by applying forces :wink:

3 Likes

Oh, good catch on that typo. And in fact, the messages should be “enable” and “disable”, so if that code works, then something is weird.

4 Likes

Thank you everyone!

3 Likes