In a 2D platformer, I would like to move the character using simple velocity relying on the physics engine. I know the documentation says it’s better to use a Kinematic object and control it manually but I find it a bit complicated and doesn’t make the experience all that different. ( I’m not very good at math or trigonometry)
Using the apply_force message is the only way to manipulate a dynamic collision object.
If you want to simplify the platformer stuff you could try a Lua module that I have for platformer games. The Lua module is in my utility lib Ludobits, and it’s used like this:
Are there any plans for adding a function that changes the velocity of an object in the next week or so? I know for a fact that Box2D supports it.
What I want to achieve here is very simple and it shouldn’t require any extra libraries. Also, apply_force is not the same as adding velocity.
No, sorry, there are no plans to add this I’m afraid (at least not in the coming weeks).
EDIT: You should be able to calculate the required force on central mass to achieve a target velocity. We provide a way to read mass and linear_velocity from a collision object. It will be a bit more cumbersome than just setting the velocity, but that’s all we can provide for now I’m afraid.
If it’s a dynamic collision object then you only have apply_force (see my previous note on mass and linear_velocity). If it’s a kinematic object then you’re free to move it using go.set_position().
If you don’t want to write this stuff yourself you should use a platformer template that someone else wrote, like the one Britzl gave you, or maybe the Platformer Creation Kit.
If you decide to do something the manual says you shouldn’t, expect it to be harder!
You can easily “set” the velocity of a dynamic physics body with “apply_force” messages. Just apply a force of:
Force = (target_velocity - current_velocity) * mass * (1/dt)
You need dt which you get from the update function, so you either need to apply the force on update, or save dt to a variable on update so you can use it elsewhere.
@ross.grams
Thanks for the reply.
I don’t want to use any kind of a library or kit, I want to do my own thing and know what each line of code does and have total control over it. Plus, I tried the “Platformer Kit” you mentioned and the character movement is horrible.( I’m guessing that’s not physics based?)
The manual did not say I shouldn’t move a character with physics, it said it’s better to use a kinematic object and change the position manually.
I still can’t find any good reason why a simple trivial thing like changing the velocity directly doesn’t exist in this engine.
Make a feature request for setting the velocity of physics objects directly.
The reason why some features don’t exist yet is there has not yet been enough demand to add different things.
It is still possible for you to code your own movement style, and then use the physics for collision detection only. Custom movements will always feel better than trying to get physics based movements to be right. https://www.pkeod.com/dive-into-defold/
Ok, So, I followed the advice and relied on manually controlling the character. I copied the code from the “Runner” tutorial to get the basic collision with the ground and jumping functionality.
I added this code for moving:
local speed = 200
local btn_pressed = false
local function move( self )
if self.ground_contact and btn_pressed then
-- set take-off speed
self.velocity.x = speed
else
self.velocity.x = 0
end
end
function update(self, dt)
local gravity = vmath.vector3(0, gravity, 0)
if not self.ground_contact then
-- Apply gravity if there's no ground contact
self.velocity = self.velocity + gravity
end
-- apply velocity to the player character
go.set_position(go.get_position() + self.velocity * dt)
move(self) << ------------------ Added this
-- reset volatile state
self.correction = vmath.vector3()
self.ground_contact = false
end
function on_message(self, message_id, message, sender)
if message_id == hash("move") then
btn_pressed = true <<-------- Start Moving
elseif message_id == hash("jump") then
jump( self )
elseif message_id == hash("released") then
btn_pressed = false <<<---------- In order to stop moving
end
end
I’m not sure If I’ve done it the right way.
Edit:
I ended up copying the code from the “Platformer” tutorial and modified it a bit to work with my project.
Lots of math calculations that I don’t fully understand, but I guess I could just tweak some of the values at the top of the script to suit my needs.