Fixed jump movement in box2d?

Hello,

I was wondering how I possibly could achieve this:


I know that there is not really any need for a physics engine to achieve this but I want the functionalities of detecting collisions so it is necessary for the purpose of the game. I want the player to end at the exact same y-position as it jumped from (which is half the height of the screen) and and a “jump movement” in a fixed curve like what is illustrated in the attached image. Many of you are probably more into chipmunk but if there is somebody out there who could help, it would really be appreciated!
Cheers

But you can use inbuilt physics engine just for collision detection. Use setCollisionBitmask, setCategoryBitmask & setContactTestBitmask properly
You can search this forum for such post.
One of this post, check this

Hmm yea but I do think that box2d would be more efficient and easy to use for me plus that I know more box2d than the inbuilt physics. I am still a lot more concerned about the actual problem - The fixed “jump movement”
Update: And I am also going to use a physics engine for making a flying scene later in the game…

My experience of Box2d (which I use in my game) is that it really isn’t good for producing repeatable events; I am guessing there is some randomness built-in (possibly to cater for things like a triangle landing exactly on its point).
Also rounding differences across machines make slight differences,
So - if you apply a force to make your player jump from left to right, in my experience they will end at almost exactly the same location each time - but not exactly the same location.
Let’s assume that “very nearly” is close enough to “exactly” for your purposes.
Now - your problem, I think, is that you need to calculate the force magnitude and angle required to send your player the required distance. Maths is your friend…

I am assuming you want the most efficient jump and there is no air resistance.

d = v^2 / g
where d is the distance you want to travel, v is the velocity and g is gravity.

Well you know d and g, so the velocity you want is given by
v = sqrt(d * g)

The angle for the most efficient jump is 45 degrees

the easy thing to do would be to set the velocity to v at 45 degrees, rather than applying the impulse to give that velocity - but the impulse is simply the mass of your body x the velocity - so it should be reasonably easy.

1 Like

Thanks buddy! But what you did was to create such action right:

instead of creating a movement that looks like a real jump or am I misunderstanding something of what you just said?

So would it be better to do this with a sprite by the moveTo action or similar and then set the position of the box2d player object to the corresponding position as the sprite? Or would that simply cause more issues?
Thanks! Your time is really appreciated!

Yes that would be easier, and you can easily accomplish the desired jump curve using Bezier.

Nope - what I described was exactly what you want - i.e. the box follows a parabola.

If you imagine hitting a ball with a bat, at 45 degrees, it will go up for 1/2 the distance it will travel, then down for the second half.

so with a physics engine you just need to calculate how hard to hit the object to do the same.

If you’re using physics for other things then its worth using. A long as you don’t mean “exactly”

@Boby @Maxxx, thank you so much for your replies!

  1. [quote=“Boby, post:6, topic:34981”]
    Yes that would be easier, and you can easily accomplish the desired jump curve using Bezier.
    [/quote]
    Boby, Yes good idea about using the Bezier but in order to make the b2Body follow the sprite, then I would probably have to use the b2 setPosition function in order to place it at the exact same position as the sprite every interval…
    I found a quote about setPosition "Obviously, the instantaneous jump means you are subverting the physics simulation but it is the simplest most direct way to set the position of a body. and so by using the setPosition, wouldn’t there occur some problems when it comes to collision detection as the fact that this function will subvert the physics stimulation??

  2. Maxxx, okay then :)[quote=“Maxxx, post:7, topic:34981”]
    If you’re using physics for other things then its worth using. A long as you don’t mean “exactly”
    [/quote]
    I know you are saying that it is not really “exact” but it not like that the player will be at the top of the screen after some time from starting the the middle with the purpose of always being at that height, right?

Don’t you just apply velocity to a body or can you also apply it at a certain degree? :no_mouth:
Finally, what are your thoughts about using Bezier and the b2 func setPosition?

Thanks guys!!!

Yes it will cause problems in your physics simulation, but if you don’t care about an accurate physic behavior and just want collision detection it’s fine.

I do want a realistic behavior I guess but aren’t “accurate physics behavior” and “collision detection” connected? And of course, thank you for the time!

Yep - the accuracy will probably be ok for you using physics - (and it’s more fun to program IMHO)

We’re talking sub-pixel inaccuracy- but obviously that can add up over time. For example I have some not-quite-circular bodies in my game - they start at exactly the same place, under exactly the same conditions, and several drop to the ground - but they don’t bounce exactly the same way every time - they do bounce realistically, but there’s subtle variation.

Velocity is speed plus direction so when you set a velocity you must set the direction by definition - so you need to set the velocity to (1,1) * magnitude.

Using setPosition in my experience has led to more problems than it’s worth. For example, if you are moving your body 10 ‘pixels’ that might take its physics body through the edge of another body - if you let the ending handle the collisions you are likely to see the movable object shooting off at high speed - so you end having to handle it yourself (like, move 7 pixels back as thats where the collision was) Box2d does all that for you if you use forces rather than set position.

1 Like

Thanks man! But how to set the velocity at a given angle?

Thanks again buddy! (I have tried to do some research)