views:

585

answers:

2

I made a game that using a character that can moving left or right by touching the screen, above the character, it is a apple, what i need is the apple will fall down, the character may push the apple up again just like playing volleyball. That's the problem,what i have learnt from "bounding ball" is give a sprite a shape and body, then they would collide automatically,when i give shape and body to the character, the animation is not work, the character moved and after a second he "jumps" to the starting point,whatever i using setPosition or Action(MoveTo) to sprite are also useless moving the body is not work too.I use touchmoved to set the body->p,it keeps shaking and when i release the button, it "fly" alway out of bound.......... Can anyone tell me what should i do or give a similar example(e.g. volleyball and something like that),Plx help!!!

I appreciate that if anyone can give me a hand.

+4  A: 

Adding a chipmunk body and shape to a sprite is not the best way to go about this. Try not to think from the screen's point of view! Instead, create a controller that contains a reference to the physics body and the sprite.

Using a physics engine, such as chipmunk, can be a bit daunting. It helps to enable debug drawing. Have a look into how you can best do that! There is some code you can take from the cocos2d chipmunk example, that comes with the normal cocos2d download)

I take it you've added a "ground" shape as a static shape and have gravity set?

When you move a body with body->p = cpv(x,y); the problem is that you don't change the objects state other than it's position. To the physics engine, it's like you teleport the object. If the object was falling, it will still be falling.

So now, your falling object is not touching the ground. Its speed increases. You "teleport" it again.. but until it bumps into anything, gravity will keep increasing its falling speed.

So, if you move an object to another point, be sure to reset all forces and the velocity on that object, if this need to be done!

A better way than using body->p is to use these functions:

  • void cpBodySlew(cpBody *body, cpVect pos, cpFloat dt)
  • void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
  • void cpBodyUpdatePosition(cpBody *body, cpFloat dt)

The documentation of these functions isn't too ridged, but there's other documentation out there. Give it a google :-p I haven't fully mastered using these functions yet either (I'm still trying to get around Chipmunk too) but they're the way to go about it.

Alternatively you can (instead of setting body->p) set the velocity of the object directly (though cpBodySlew does this for you)

nash
+1  A: 

The latest chipmunk code is doing this with a pivot joint in between the object to be grabbed and the mouse (touch): ChipmunkDemo.c.

See also: Mouse interaction

diciu