• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Magnetic Coin Effect

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1mohan123  Members

Posted 08 May 2012 - 07:49 PM

I am developing physics based game using box2d in cocos2d-iphone. Can any one please help me in creating magnetic coin effect as it is in Temple run and Jet Pack game. In my game my character is a movable plane and the magnetic power appears from opposite direction and once it is collected then its effect remains for few seconds. And in that effect time if any coins are coming from opposite side of the plane then those coins should get attract towards the plane. And those coins are also a box2d body. Right now i am using the SetLinearVelocity to move it to the left end of the screen only changing the x position. In this the once the y position is set its not able to change it. So please any one can help me in this to make magnetic effect work. Any kind of help will be appreciated. Thank you very much.

### #2ndssia  Members

Posted 09 May 2012 - 12:52 AM

Please show a code snippet.

### #3mohan123  Members

Posted 09 May 2012 - 05:22 AM

hi ndssia

i have a coin class in which i am creating a body of coin and giving it a linear velocity (because my plane's x-axis is constant and background is panning) to move code is:-

-(void) createCoin:(ActionManager*)_objActionManager:(float)_x:(float)_y:(int)_no
{

b2BodyDef coinBodyDef;
coinBodyDef.type = b2_kinematicBody;
coinBodyDef.position.Set(_x/PTM_RATIO, _y/PTM_RATIO);//_x/PTM_RATIO, _y/PTM_RATIO
coinBodyDef.userData = s_coin;
//coinBodyDef.linearDamping = 1.5f;//2.7
//coinBodyDef.angularDamping = 1.5f;//3.0
m_coinBody = _objActionManager->world->CreateBody(&coinBodyDef);

b2CircleShape circle;

b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;//1
ballShapeDef.friction = 0.7f;//0.7
ballShapeDef.restitution = 0.0f;//0.5
ballShapeDef.isSensor = TRUE;
m_coinFixt = m_coinBody->CreateFixture(&ballShapeDef);

float speed = _objActionManager->m_obstaclesSpeed;

m_coinBody->SetLinearVelocity(b2Vec2(-(speed),0));
}

and m calling this function in my main game play layer (Action Manager)

-(id) init
{

if( (self=[super init]))
{

if(!m_objCoin)
m_objCoin = [[Coins alloc]init];
}
}

now i am calling my createCoin function with the help of m_objCoin when i need to show coins:

[m_objCoin createCoin:self :550 :_y :1];

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.