How to make object jump?

Official forum for the Chipmunk2D Physics Library.
Post Reply
X-0ut
Posts: 28
Joined: Tue Oct 02, 2007 5:02 am
Contact:

How to make object jump?

Post by X-0ut »

I've tried using both cpBodyApplyImpulse and cpBodyApplyForce to make an object jump upwards, but it allways moves sideways.
Is this the way I'm supposed to do this or is there a better way?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to make object jump?

Post by slembcke »

Don't know why that would make the object move sideways.

I usually just do something like the following:

Code: Select all

body->v.y += 20.0f
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
X-0ut
Posts: 28
Joined: Tue Oct 02, 2007 5:02 am
Contact:

Re: How to make object jump?

Post by X-0ut »

Oh ok thanks, in the docs it says not to set those values explicitly so I did not bother trying - might want to update them.
I also had to make it so that the object only jumped when on land, which I did by using a collision pair function. (Maybe that might help someone in future, I don't know)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to make object jump?

Post by slembcke »

X-0ut wrote:Oh ok thanks, in the docs it says not to set those values explicitly so I did not bother trying - might want to update them.
I also had to make it so that the object only jumped when on land, which I did by using a collision pair function. (Maybe that might help someone in future, I don't know)
Setting the position constantly is generally a bad idea. Setting the velocity can be fine if you are careful (cpBodyApplyImpluse() does nothing special in this regard). The problem is when you do something like this:

Code: Select all

body->v.x = 100
Say that object hits a solid wall. The wall will stop it's velocity, but the next step it will get set back to 100. This means that the velocity will be flickering between 0 and 100 while the impulse solver is running. Anything else touching that object is likely to behave very oddly.

Yeah, a collision function is the only good way to do it. You will also want to use the collision normal to determine if the player is touching a wall, the ceiling, or the floor.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
X-0ut
Posts: 28
Joined: Tue Oct 02, 2007 5:02 am
Contact:

Re: How to make object jump?

Post by X-0ut »

Thanks, it all worked out great :)
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests