Better way to move dynamic collision objects smoothly

Hello! I have a kinematic character. And I want to make him move a box. Box has dynamic collider. If I leave things as is box is moving when character reaches it but it doesn’t look beautiful. Box moves with jerks. Is there any best practices for moving dynamic objects which interact with kinematics?
I am moving an character with this code:

local pos = go.get_position() + self.velocity * dt
go.set_position(pos)

Ooops… I’ve found a simple solution :smile:
May be everybody knows about it, but I’ll repeat. If you cache your position everything looks ok. So code should be like this:

function init(self)
    self.pos = go.get_position()
end

function update(self, dt)
    self.pos = self.pos + self.velocity * dt
    go.set_position(pos)
end

Hmm, I don’t see how caching the position should make any difference…

PS Shouldn’t it be go.set_position(**self.**pos) in your code?

1 Like

Yes, sure. It is a mistake.
About my solution. It works for moving dynamic objects but it breaks a lot of other stuff (interacting with static objects, moving my character by other kinematic objects) and now I’m trying to find another solution.
Caching a position works for dynamic objects and makes a lot of problems for other behaviour because I calculate next position from my previously cached position, doesn’t matter which position is actual right now for character.
I’ll try to explain this.

  1. Cached position.
    Let’s assume that initial self.pos = (0,0,0)
    After first tic it will be (dt, 0, 0) if self.velocity = (1,0,0)
    If on the first tic I’ve got a static object in front of me next position will be calculated from self.pos and next position from previous self.pos etc… With this behaviour game looks like character can move through collider. It is bad.

  2. Position got from go every frame
    Let’s assume the same. Initial self.pos = (0,0,0)
    If on the first tic I’ve got a static object in front of me next position will be calculated from object actual position and next position again from object actual position etc. It all looks like collider can not be at middle position with other collider and when it reaches the border of other collider it keeps its position. But If game object has position where whole collider is inside other collider, other collider stops to prevent entering in it.

It all sounds a little bit messy, but I hope that you’ll understand a problem. Here is a picture about it.

So, ok. Let’s continue subject with title ‘stupid lame stuff’. :smile:
I figured out what happens here. Everything because of handle_geometry_contact function from tutorial. If I change go.get_position() to self.pos in it everything starts to work as expected, but with it we returned to what I start from - problem with smooth movement of dynamic object.

Don’t waste your time with contact_point_response and related stuff.
Use raycasts. This is not simple because of horrible design mistakes in Defold (deferred raycasting, absence of nothing-is-hit response) but this is still much better than fighting with physics.

What do you mean when talking about raycast? Can you share any code or pseudocode how would you implement dynamic objects moving?

I assume you want movable box, no matter how it implemented, dynamic or kinematic. Am I right?
I think, great platformer mehanics (stable, predictable) can’t be achived with physics.

I will post example project when I clean it up from some unrelated stuff. For now it too deeply integrated into other experiments.
However, you can see how it behaves here https://nsobject.itch.io/test-subject. It is little jerky on html5 but on mobiles (5s) all is fine.
Controls: left, right, space. Hit escape key and press restart button to restart simulation. Movable box sits on top of two vertically moving platforms.
No dynamic objects, only kinematic ones moved by doing raycasts and analyzing responses.

2 Likes

Yes, sure! I don’t exactly know how to make it the right way. I need to be available to move box and I want to make the box behave like a physics object - so if box pushed from platform it should fall and rotate.

Seems like the link is broken. Thanks!

Sorry, here is the right link https://nsobject.itch.io/test-subject?secret=44Ko3r2zTkXH8ouMPp0T0NiQSw