Box2D Forums

It is currently Thu May 23, 2013 5:05 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Magnetic Coin Effect
PostPosted: Tue May 08, 2012 7:46 am 
Offline

Joined: Tue May 08, 2012 7:36 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Magnetic Coin Effect
PostPosted: Tue May 08, 2012 12:25 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
How about applying a force to the coins to push them toward the plane. You could make the force stronger as the coins got closer.
This is how gravity works and as you know, gravity makes things orbit around and around, so you would probably want to add a little bit of linear damping to the coin bodies too.


Top
 Profile  
 
 Post subject: Re: Magnetic Coin Effect
PostPosted: Wed May 09, 2012 4:28 am 
Offline

Joined: Tue May 08, 2012 7:36 am
Posts: 2
Hi

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group