I have a problem with re-bounce of two balls.

Official forum for the Chipmunk2D Physics Library.
Post Reply
guru
Posts: 1
Joined: Wed Dec 09, 2009 12:34 am
Contact:

I have a problem with re-bounce of two balls.

Post by guru »

In my program i have to re-bounce the two Balls, but every time of re-bouncing, the speed of balls comes faster and faster every time.So my question is what i do, so the speed of ball remain constant in every time of re-bouncing. (Two Balls:- (1) ball1 (2) slider_ball) Code given below:-

-(void)setupChipmunk
{
cpInitChipmunk();
space = cpSpaceNew();
space->gravity = cpv(0,-100);
space->elasticIterations = 1;
/////////////////////////////// Ball Body & Shape //////////////////////////////
ballBody1 = cpBodyNew(200.0, INFINITY);
ballBody1->p = cpv(160, 20);
ballBody1->v = cpvmult(cpvforangle(45),100);

cpSpaceAddBody(space, ballBody1);
ballShape1 = cpCircleShapeNew(ballBody1, 20.0, cpvzero);
ballShape1->e = 1.5;
ballShape1->u = 0.0;
ballShape1->data = ball1;
ballShape1->collision_type = 2;
cpSpaceAddShape(space, ballShape1);

floorBody = cpBodyNew(INFINITY, INFINITY);
floorBody->p = cpv(0, 0);
floorShape = cpSegmentShapeNew(floorBody, cpv(0.0,0.0), cpv(0.0,480.0), 0);
floorShape->e = 0.5;
floorShape->u = 1.5;
floorShape->collision_type = 1;
cpSpaceAddStaticShape(space, floorShape);

floorBody1 = cpBodyNew(INFINITY, INFINITY);
floorBody1->p = cpv(0, 0);
floorShape1 = cpSegmentShapeNew(floorBody1, cpv(0.0,480.0), cpv(320.0,480.0), 0);
floorShape1->e = 0.5;
floorShape1->u = 1.5;
floorShape1->collision_type = 1;
cpSpaceAddStaticShape(space, floorShape1);

floorBody2 = cpBodyNew(INFINITY, INFINITY);
floorBody2->p = cpv(0, 0);
floorShape2 = cpSegmentShapeNew(floorBody2, cpv(320.0,480.0), cpv(320.0,0.0),0);
floorShape2->e = 0.5;
floorShape2->u = 1.5;
floorShape2->collision_type = 1;
cpSpaceAddStaticShape(space, floorShape2);

floorBody3 = cpBodyNew(INFINITY, INFINITY);
floorBody3->p = cpv(0, 0);
floorShape3 = cpSegmentShapeNew(floorBody3, cpv(320.0,0.0), cpv(0.0,0.0),0);
floorShape3->e = 0.5;
floorShape3->u = 1.5;
floorShape3->collision_type = 1;
cpSpaceAddStaticShape(space, floorShape3);

for(i=0 ; i < numberOfEnemy ; i++ )
{
int rand=arc4random()%numberOfEnemy;

enemy [ i ] = [Sprite spriteWithFile:@"slider_ball.png"];

[enemy [ i ] setPosition:ccp(initialPosX[rand],initialPos)];

[self addChild:enemy [ i ] z:0];

enemyStack[ i ].EnemyBody = cpBodyNew(200.0, INFINITY);

enemyStack[ i ].EnemyShape = cpCircleShapeNew(enemyStack[ i ].EnemyBody, 20.0,cpvzero) ;

enemyStack[ i ].EnemyShape->u =0.0; // Friction

enemyStack[ i ].EnemyShape->e=1.5;

enemyStack[ i ].EnemyShape->data = enemy; // Associate with out ball's UIImageView

enemyStack[ i ].EnemyShape->collision_type = 1+i; // Collisions are grouped by types

enemyStack[ i ].objectAngle=0.0f;

}


for(enemyCounter=0;enemyCounter<numberOfEnemy;enemyCounter++)
{
initialPos+=20;
int PosX = ( arc4random() %10 );
enemyStack[ enemyCounter ].EnemyBody->p = cpv( initialPosX[PosX], initialPos ) ;
cpSpaceAddBody( space, enemyStack[ enemyCounter ].EnemyBody) ; // Add the body to the space
cpSpaceAddShape(space, enemyStack[ enemyCounter ].EnemyShape); // Add the shape to out space

}
temp=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

-(id)init
{
if(self==[super init])
{
ball1 = [Sprite spriteWithFile:@"ball1.png"];
[ball1 setPosition:ccp(160, 20)];
[self addChild:ball1 z:0];
initialPosX[0]=110;
initialPosX[1]=130;
initialPosX[2]=150;
initialPosX[3]=170;
initialPosX[4]=190;
initialPosX[5]=210;
initialPosX[6]=230;
initialPosX[7]=250;
initialPosX[8]=270;
initialPosX[9]=200;
initialPos=400;
enemyCounter=0;
[self setupChipmunk];
}
return self;
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I have a problem with re-bounce of two balls.

Post by slembcke »

Chipmunk calculates how elastic a collision should be by multiplying shape.e together for both shapes. If that multiplied value is 0, then there is no bounce, if the value is 1 it's a perfect bounce with no speed lost. If it's greater than 1, then you will start gaining speed from collisions.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests