Anyone using CCActions in a game that also uses Box2d or Chipmunk physics? For example, you have a platform game with dynamic objects such as moving platforms or enemies, each of which has a physics body. For each, you want to be able to take advantage of CCActions but still use the physics engine. Anyone doing this, and if so what is the best way to update your bodies? I would assume subclass CCSprite, but is that the best?
Using CCActions with Box2D or Chipmunk?
(10 posts) (7 voices)-
Posted 1 year ago #
-
I created a custom class called
Actorwhich has ab2Bodyand aCCSpritevia composition. Every time my game runs through thetick:method I call an update method on each actor which puts the sprite over the body. I don't useCCActions as b2Body represents the position of the sprite. If I move the sprite somewhere else via an action, it would only be sprung back to where theb2bodyis the next frame.If you want to use
CCActions, you can create subclasses of the ones you desire to use and have them control theb2Bodyinstead.Posted 1 year ago # -
the spacemanager demo shows how to do this with Chipmunk and actions... in the demo, on the right, you'll see a block moving back and forth, left to right and vice versa.... that one's moved by actions:
Posted 1 year ago # -
Hi,
I wondered how to do this also. What I've resorted to is to just not bother either using CCActions with Box2d/CCSprites and those I want to use actions on don't have Box2D bodies attached to them - of cos, this means using alternate methods for collision.
I think robodude666 offers a better alternative, i.e. subclass the CCActions you want, and get them to move the box2d body instead of the sprite...anybody done this?
Regards,
StevePosted 1 year ago # -
This has been asked and answered many times before. Search for "override setPosition" or "setPosition" or "action and physics".
Posted 1 year ago # -
Just in case the answer is still ambiguous, it's a definite YES. It's a simple case of overriding the setPosition/setRotation in your CCNode and forcing the chipmunk/box2d body to stay in sync.
Posted 1 year ago # -
robodude666 - That is an awesome idea and now I feel stupid because its perfect, move the body not the sprite, then just get the sprite position back from the physics engine. Have you had any problems with doing this?
Posted 1 year ago # -
Nope. I actually do this for moving platforms. Here's an example of a platform that moves horizontally on the x-axis between two bounds:
if([mySprite position].x >= rightBounds){ direction = -1; }else if([mySprite position].x <= leftBounds) { direction = 1; } #define PIXELS_TO_MOVE 2 b2Vec2 desiredLocation = b2Vec2([mySprite position].x + (direction * PIXELS_TO_MOVE), yPos); b2Vec2 directionToTravel = b2Vec2(desiredLocation.x - [mySprite position].x, desiredLocation.y - [mySprite position].y); directionToTravel *= 60 / PTM_RATIO; // 60 frames per second, PTM_RATIO is 32 pixels per meter myBody->SetLinearVelocity(directionToTravel);Where
directionis just a member int,yPosis a value in pixels of the y position to maintain.All it does is calculate a new position PIXELS_TO_MOVE pixels away from the current position, find the distance that needs to be traveled to reach that position and convert it to meters per frame. Then I simply set the linear velocity and update it again next frame.
Note that I'm using the sprite's position as it's already in pixels. Later on I move to sprite to the body's location * PTM_RATIO. I could use the body's position here, but then I'd have to add a ton of PTM_RATIOs into the equations which would complicate things.
You can set this up as a generic method that accepts a body, a sprite and a velocity to move at and have it do the rest for you. The velocity to move at would be based on the CCAction that it is undergoing. I'd personally write 100% my own classes to do this instead of messing with CCAction unless it can easily be subclassed. I'm only doing this within one of my classes, so I won't bother with it yet.. however, I'll soon be adding multiple types of enemies/objects that can move on their own and will feature this into my root
Actorclass instead of the specific platform class it's currently in.Posted 1 year ago # -
i am using cpShapeNodes in spacemanager but i am not able to attach sprites on these nodes as the userData property of the nodes are always initiated as nil can anyone help me out???
Posted 1 year ago #
Reply
You must log in to post.