Skip to main content
GameDev.net gamedev.net
🔒 Locked

Magnetic Coin Effect

Started by h7c97 May 9, 2012 at 1:49 AM 1 replies 2.2k views
Original Post
h7c97
h7c97
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.
ndssia
ndssia
Please show a code snippet.
h7c97
h7c97
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;
circle.m_radius = 10.0/PTM_RATIO;

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];

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.