Am a flash game developer at present I started writing games for iphone using cocos 2d engine, I have implemented separate Axis theorem for collision detection which works perfect. All the polygons are drawn as follows in openGl. Now am trying to apply gravity to this 16x16 box after many search I found this tutorial http://www.seinia.com/tutorials/Bounce/ and implemented the same in objective C.
The problem am having is after the square comes to rest it keeps bouncing up/down in fractions. I tried a lot to fix this but I couldn't control that tiny movement.I never had such problem in flash but here the floating value is affecting the square position a lot.
Please let me know what is the write way to handle such issue , any reference URL would be helpful. Appriciated your help.Thanks!
0,16 16,16
------------
| |
| |
| |
| |
--开发者_开发技巧----------
0,0 16,0
Objective C code
if (square.points[0].y <= 0.1f) {
velocity.vy *= -bounce;
[square restInPeace:[Vector2D createVectorWithComponentX:velocity.vx Y:8.0f]];
// landed
if (fabs(velocity.vy) < 0.9f) {
velocity.vy = 0.0f;
[square restInPeace:[Vector2D createVectorWithComponentX:velocity.vx Y:8.0f]];
isLanded = YES;
}
}
Translate the object
-(void) translateVelocity:(Vector2D*)velocity
{
// The center as well as all of the vertices need to be
// accommodated.
center.x += velocity.vx;
center.y += velocity.vy;
for(int i=0; i < count; i++)
{
points[i].x += velocity.vx;
points[i].y += velocity.vy;
////NSLog(@"velocity %f %f",points[i].x , points[i].y);
}
}
When using a bounce algorithm, it is usually recommended to implement a slight imperfection to make sure that this event does not happen. You could also largen the range of what is accepted as "Landed", but remember to make sure that you then stick the object to the floor to make sure there are no visual artifacts.
By the imperfection I mean :
velocity.vy *= (-bounce + 0.01f);
For example. This should make your object always come to a halt.
精彩评论