How to make a kinematic object act like a dynamic object?

Beginner physics question!

I’ve learned that dynamic objects resolve physics on their own and are useful for Angry Birds-type of games etc. But for platformers and many other scenarios you’re recommended to use kinematic objects instead since it gives you more control.

I wonder if anyone know if it’s possible - and then how - to code a game object with a kinematic collision object to act like a dynamic collision object. This would be a very good starting point to learn from and modify.

I often find myself wanting to have an ‘almost dynamic’ type but with slight modifications. I’ve looked at the platformer tutorial(s) and tried to start from that, but quite simply overwhelmed and not very good at physics math. Not even sure if I should use the self.ground_contact bool or not.

If someone knows how to do this it would for sure help me, but maybe someone else too :slight_smile:

1 Like

I am currently looking into my own physics stuff. It is completed (although I am having coding complications, rather than phsyics complications… so far)

The manual has some quite useful information about resolving collisions.

I would say that it depends on what you wish to do. Do you wish to do a platformer with typical platformer physics (gravity, double jump, wall jump etc), a Thrust kinda game (gravity, rotation, thrust and drag), a top-down racing game or something else? Sure, you could implement a full physics system using kinematic objects, but it will become a fairly complex system.

Now, I’ve wrapped up most of the logic required for a platformer game into a platformer.lua module:


https://github.com/britzl/ludobits/tree/master/examples/platformer (example)

If you want a Thrust type of game I think you should take a look at the code for Rocket Run by Ben James.

And if you’re interested in a racing game maybe look at Pastel Riders by Mathias Westerdahl

Maybe I’m not fully grasping how the physics system works here, but what I was thinking was something along the lines of:

Set up some static collision objects. Add say, a ball, as a dynamic object. It could roll down a slope, bang into a wall and eventually stop.

Now, let’s add another game object with a kinematic collision object. Insert “secret sauce code” into the script of this game object. Add this, also in the shape of a ball of same size and mass/etc as previous ball, would now result in identical reactions as the dynamic object.

My reasoning behind this is that since dynamic objects already exist, the “secret sauce code” is in fact not a mystery, but something that has been implemented in a multitude of languages/scenarios/code examples.

But maybe I’m thinking about this the wrong way - would the code perhaps be far too complex? Yet the dynamic objects do exist and work fine… hmm.

britzl, to tag on to your analogy, imagine the game I would like to do is one where my kinematic object acts exactly like a dynamic one :smile:

Edit:
Actual example I’m trying to achieve, apart from needing this in general:

Imagine a pinball simulation. Dynamic object works great for the ball, for example. Now, I want to be able to control this ball in my type of game. By the push of a button I want to completely freeze the velocity of the ball (impossible with a dynamic object but not with kinematic), and then apply an impulse force to the ball (possible with dynamic objects).
Except for the ‘freeze’ before impulse shock, it should act as dynamic object in general.

If i were to do this in Stencyl, I would kill the dynamic object and spawn a kinematic object with the same animation at the same location, and vice-versa. They would be two distinct objects with the same animation.

Yes, the “secret sauce” as you call it has been implemented in a multitude of languages. For instance C, which is used by Box2D, which is used by Defold. Why write all of that code in Lua again?

When you say “freeze” the ball, do you then mean negating any linear velocity and negating the gravity, or something else? You could perhaps also temporarily disable the collision object, but that wouldn’t reset any forces applied to it, but it would temporarily freeze it.

The reason for writing it in Lua is to have a good starting point from which one can add (or subtract) functionality.
For example, I want to have an object which acts exactly like a dynamic object but with a small change. The small change = not the real challenge for me. Act exactly like dynamic object = real challenge.

Do you happen to know if kinematic vs dynamic objects are Defold-specific or terms inherited from Box2d?

“Kinematic”/“static”/“dynamic” are common terms in various physics engines.

I doubt that you can get the “exact” behaviour that you are looking for, since the kinematic body is deliberately bending the rules of the dynamics solving. For instance, your kinematic body has infinite mass, which the dynamic bodies doesn’t. You can set the position, which gives you the opportunity to put your body in the middle of a bunch of dynamic objects, which (usually) doesn’t happen for dynamic objects. And, your body doesn’t respond to forces. And finally, updating the kinetic object happens after the solving step, not during it. All in all, I think it’s very difficult to make them behave exactly the same. Personally, I’d go for “similar, under certain conditions” :slight_smile:

EDIT:
Here are some links to bullet and Box2D (search for “bodies”) as a starting point.

5 Likes

Ok thank you, I was thinking I was probably coming at it from an uninformed angle. Makes sense, what you’re saying!

Now I can’t really decide though… if I should try a ‘hacky’ way of resetting the velocity of my ball by switching out the game object, or try to make a kinematic object act like a bouncing pinball…

Will have to laborate!

I think there are many games where physics rules are broken by player characters (mario putting on a flying suit, or ducking down and thus being able to travel through a normally solid pipe). In my game I switched out the character during a death animation (making him float away into space, whereas normally he couldn’t rotate and had gravity)

1 Like