2
\$\begingroup\$

how can i move static box2d sprites. i have tried this tutorial from . http://www.raywenderlich.com/475/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-12.

I managed to add another "paddle" object with box2d body, but i can seam to be able to make the code to move the second "paddle" body. Can anyone direct me how to do it?

Is there a way to move a "b2_staticBody" box 2d object? i have tried, but i can only move it when i use "b2_dynamicBody" if i used "b2_staticBody" i can move it at all.

\$\endgroup\$
1
  • \$\begingroup\$ "moving" and "static" are kinda mutually exclusive, so what you need is some other kind of object if you want to move it. \$\endgroup\$ Commented Feb 8, 2011 at 8:24

2 Answers 2

1
\$\begingroup\$

You want to use a dynamic body, if you want bodies to move in your physics. That is, the nature of bodies in box2d. You make a dynamic body, and apply impulse or force to it in the direction you want it to move.

I recently wrote this very basic tutorial to create a pong game using Farseer physics engine (which is a C# port of Box2d), so the same things would apply.

Also, it would help to know what kind of a game are you making ?

\$\endgroup\$
2
  • \$\begingroup\$ hi, my game will a little like a puzzle. There will be a ball, and another sprite/object. This object can be moved with a touch, and once placed i a suitable location that object should be permanent there. When i launch the ball, this ball will start to deflecting from this object to the next destination/location. Is there another method i can used for the sprite/object to be able to bounce/deflect the ball. Sorry im still new to game programming. \$\endgroup\$
    – user5198
    Commented Feb 6, 2011 at 10:13
  • \$\begingroup\$ If you're using box2d, your ball should be a dynamic object. I'd recommend making a simple (static) box. ApplyLinearImpulse(..) to the ball in the direction where the box is placed and box2d should handle the ball to bounce/deflect of the box. Also, don't forget to set the 'restitution' for the ball and box. \$\endgroup\$ Commented Feb 6, 2011 at 18:45
0
\$\begingroup\$

I'm not sure which version you are working with, but version 2.1.0 has 'kinematic bodies'.

Instead of bodyDef.type = b2_dynamicBody; you would put bodyDef.type = b2_kinematicBody;.

They are sort of like static bodies, only you can move them. They only move how you tell them to, so they aren't affected by collisions or other forces (like gravity).

From the box2d docs at http://www.box2d.org/manual.html

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other static or kinematic bodies.

\$\endgroup\$
3
  • \$\begingroup\$ Hi, can i confirm again, the b2_kinematicBody i can used it in ccTouchesMoved right? Do i need to use any mouse node like in example tutorial raywenderlich.com/475/…. Sorry im really new in Iphone SDK programming \$\endgroup\$
    – user5198
    Commented Feb 8, 2011 at 13:54
  • \$\begingroup\$ It might not be best to use a mousejoint to move a kinematic body. Check out this thread for a good discussion about this. cocos2d-iphone.org/forum/topic/7599 \$\endgroup\$
    – timrwood
    Commented Feb 12, 2011 at 23:02
  • \$\begingroup\$ Sorry, to answer the original question in your comment, yes you can move it in ccTouchesMoved. \$\endgroup\$
    – timrwood
    Commented Feb 12, 2011 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .