Scale or create cpShape base on cocos2d CCScaleTo action?

Official forum for the Chipmunk2D Physics Library.
Post Reply
rainyday
Posts: 5
Joined: Wed Mar 06, 2013 7:45 am
Contact:

Scale or create cpShape base on cocos2d CCScaleTo action?

Post by rainyday »

can we scale or create new cpShape base on cocos2d CCScaleTo action for a Sprite?

id scaleUp=[CCScaleTo actionWithDuration:0.5 scale:1.50];
id scaleDown=[CCScaleTo actionWithDuration:0.5 scale:0.50];

[sprite runAction:scaleUp];
[sprite runAction:scaleDown];
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by slembcke »

Chipmunk shapes cannot be trivially changed. Chipmunk is a rigid body physics engine (rigid meaning that shapes do not deform).

You can use the functions in chipmunk_unsafe.h to modify an existing shape, but make sure to read and understand the comments in it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rainyday
Posts: 5
Joined: Wed Mar 06, 2013 7:45 am
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by rainyday »

so the safe way is to keep remove all the shape and create new cpShape with new size to the cpBody when schedule update?

the scenario is the bullet increasing size while moving.
will it be heavy resources consuming?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by slembcke »

You don't want to replace the shape. That is going to make things slightly worse. It resets the contact cache when you replace a shape and has to do a bunch of other work it wouldn't normally need to. I'm not sure if it would noticeably worse if that's really an easier solution for you though. I have a hard time believing that it's not easier to just resize the existing shape though.

Performance isn't going to be a problem unless there are hundreds of them I would guess.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rainyday
Posts: 5
Joined: Wed Mar 06, 2013 7:45 am
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by rainyday »

so your suggestion is better to change the shape than creating a new one,right?
i just thinking is there a better way since changing the shape is define as unsafe.

/// Chipmunk does not have any way to get velocity information on changing shapes
may i know what does this mean?

cpShape isnt just use for collision detection?
it would affect the velocity of the cpBody?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by slembcke »

Replacing a shape with a different sized/shaped one has the exact same problems.

Think of an expanding ballon. It's center is not moving so its velocity is 0. It's surface, which is colliding with other objects, is moving outwards though. What those collisions need to know is that things should be getting pushed away from the center of the balloon. Instead, each frame they find that they are overlapping the balloon by some amount. Chipmunk will push them out of the balloon to fix, but not in a way that looks correct because it doesn't have any information on how the surface is moving. It only knows that the balloon's center is not moving.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rainyday
Posts: 5
Joined: Wed Mar 06, 2013 7:45 am
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by rainyday »

let me see whether i understand correctly

a force apply to the balloon center
cpBodyApplyForce(balloon, force, CGPointZero);

as normal, the balloon should expand evenly in every direction
but chipmunk wont because it dont know which direction to apply force and how many force should apply
so we cant use chipmunk to simulate the expanding balloon


for my case, i just want the bullet increasing size in sprite and collision area while moving with a constant velocity

so is safe to use this?
cpPolyShapeSetVerts(bullet, numVerts, newVerts, CGPointZerooffset);

for constant velocity, i should use body->v instead of cpBodyApplyForce,right?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by slembcke »

No, if you apply a force to the center of an object it will move in the direction of the force with no rotation.

Chipmunk is a rigid body physics engine and *only* supports rigid transformations (translation + rotation). It simply doesn't know what to do if you scale the object or do anything else beyond that. Your expanding bullet is no different than the ballon. cpPolyShapeVerts() is one of the "unsafe" functions and will have the same problems.

This only applies to the collision response though. The collisions will be caught just fine, it just might give you a strange collision response. It might work out just fine for your bullet. You really just have to try it and find out.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
rainyday
Posts: 5
Joined: Wed Mar 06, 2013 7:45 am
Contact:

Re: Scale or create cpShape base on cocos2d CCScaleTo action

Post by rainyday »

i have try using remove shape and create new one, it work fine when collision with enemy
but if i use cpPolyShapeVerts(), i cant see the red shape under debug mode

there is a strange thing when the bullet bound some times after hitting wall without hitting enemy,
the shape will scale down or scale up even the sprite stop scaling
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests