I’m currently attempting to make a platformer game in Defold and I’m finding it somewhat confusing. I looked at the platformer project that is provided and I didn’t really understand much of the code. I did read the tutorial for it too but that doesn’t help all that much either.
I understand the reason for why you chose to use a kinematic collision object for the “hero”, setting him to be kinematic means that you’re able to control how the collisions work a lot better. But this involves a lot of maths and code that I simply do not understand just to detect collisions and the code is way too complicated for being a beginners tutorial.
The game that I’m making is a mario-like game so I just want a very simple physics engine. When you press right, the character moves right at 4 pixels per second, you don’t get the acceleration thing. When I jump, I want it to jump up to a certain height that doesn’t depend on how long you hold down the button.
I’ll work with the code more tomorrow and see if I can figure this out but I’d just like to say that your platformer tutorial is very hard to understand and too complicated. It would be preferable to have a platformer tutorial (or at least some source code) that had the bare bones of a platformer where I can then start adding features.
You can definitely create something simple with dynamic physics objects and forces, but remember that the physics engine will handla all character movement based on gravity, weight, friction and restitution. You can push your hero around with forces but those have to be tuned to get the speeds and heights you want. It’s great fun experimenting with that so I really encourage you to give it a go.
Yeah, I understand I can do that but I’m wanting to know how. I probably didn’t express this issue so much in my post but I don’t understand how to apply forces. In every other game making program that I’ve used I can manually change the x and y positions and the collisions work with that, but with Defold I’m not able to do so. I can change the x and y positions if I’m using a Kinematic collision object but then I have to handle all of the collisions myself which gets messy, and then when I change the object to Dynamic my code no longer works.
You’ve got plenty of references to how both of them work, but you’ve got nowhere (that I can see at least) where it actually explained how to use them.
The only way to move dynamic objects is by applying forces. You do that by sending a message to the collision object component of the object you want to move. See http://www.defold.com/ref/collision-object#apply_force
-- apply a force of 1 Newton towards world-x at the center of the game object instance
msg.post("#co", "apply_force", {force = vmath.vector3(1, 0, 0), position = go.get_world_position()})
This assuming that the script is in the same game object as the collision object component.
yeah, I saw that line of code in the references but you don’t really explain what it does all that well. I assumed that the “force” is the amount of force you’re applying, but I wasn’t sure why you use 3 different numbers and what they were all used for. I eventually figured out the first two numbers are horizontal and vertical force, but the third number still confuses me.
And then there’s other stuff like the Restitution. In the documentation it says that if your Restitution is set to 0 then the object won’t bounce, yet my object is still bouncing off walls and stuff.
I’m not sure what it is but this physics engine seems overly complicated. All I want is to have a few lines of code that stops my Kinematic object from penetrating other objects (and another few to detect whether I’m on the ground or not) and then I can handle all of my gravity and movement from there without the help of the built-in physics engine.
Maybe for the more experienced programmers using Box2D would be good, but I’ve been using Game Maker for the past 3-4 years where I’m programming in my own gravity and movement and then there’s only 2-3 line of code which stops me object from moving through other objects.
The force you apply is a vector which describes the direction and amount of force (the length of the vector). The target position of the force is another vector. Note that these vectors are 2d for Box2D so the z component should be 0. If you turn on 3D physics (Bullet) you will need to create 3D vector that uses the x, y and z components.
Restitution is calculated from both colliding objects. If your walls have non zero restitution anything will bounce so check the restitution of your walls.
The problem with object separation is that it can be done in a great number of ways. For instance, I have a game I’m working on where the collisions include bounce and friction, so velocity is affected. For other games you might want to have detailed control over other aspects to, for example, create wall sliding like in a Mario game.
It may be that we can simplify some of this stuff but we want to do it in a game-agnostic way so you are never impeded by the engine.
Thanks for the the help, I’ll definitely check my wall’s restitution. I know that you’re wanting to keep it so that the programmer has completely control over the physics engine, but I think that there’s still a little too much complexity for beginners like myself.
One thing that I think you could add to a Kinematic object is to have a couple already built-in function to handle movement and collision. A few of these functions could be “move_x”, “move_y” and “collision_stop_movement”. The move_x and move_y function will act similarly to getting the current position, adding the new x or y value and then setting the current position. But it will also work with the collision_stop_movement function where if you detect a collision between the player and a wall, the collision_stop_movement function will prevent you from moving based on the direction of the collision.
If you want, you can also have have stuff like bouncing and friction in there too.
I think a better example would be to have a collision class, similar to your go class, and have built-in functions like “collision.stop_movement” and other variables which can be set like “collision.restitution” and “collision.friction”. And don’t forget a boolean saying whether your object is currently touching another object.
You can probably ignore me on this, you’ve been working on Defold for many years so I’m sure you have your reasons for using the physics engine like you do But just from my perspective this would make things so much easier to understand and implement basic physics, especially for beginners.
I think what you are describing Listrix would go very well into a reusable Lua module. There is no real need to put this into the engine. Once you feel confident enough working with the physics in Defold maybe you can create such a module yourself and share it with the community? Or maybe someone already has a module to share?
I’ll definitely work on making a platformer and when I have a solid engine I’ll start sorting out my code and putting them in to reusable functions such as this. Might take a while until I get to that stage but I’ll try and get there.