Im new to chipmunk, and I'm working on getting a ball to bounce off the wall, change direction, and never stop. I read on the forum to use cpMomentForCircle with the same radii and and no offset for a ping pong effect, but I'm not having any luck. I'm under the impression friction makes a difference, but setting it to 0 didn't do anything for me. At best I got it to bounce a few times then go off the screen for good. If anyone could just point me in the right path that would be great, not necessarily looking for code.
Thanks
Random bouncing ball with no friction
-
- Posts: 4
- Joined: Wed Jun 10, 2009 12:22 pm
- Contact:
-
- Posts: 71
- Joined: Sat Dec 20, 2008 12:16 pm
- Location: Minnesota
- Contact:
Re: Random bouncing ball with no friction
Do you mean something similar to like a brickout-type game but without any bricks or paddles?TheServo wrote:Im new to chipmunk, and I'm working on getting a ball to bounce off the wall, change direction, and never stop. I read on the forum to use cpMomentForCircle with the same radii and and no offset for a ping pong effect, but I'm not having any luck. I'm under the impression friction makes a difference, but setting it to 0 didn't do anything for me. At best I got it to bounce a few times then go off the screen for good. If anyone could just point me in the right path that would be great, not necessarily looking for code.
Thanks
What platform are you developing for?
-
- Posts: 4
- Joined: Wed Jun 10, 2009 12:22 pm
- Contact:
Re: Random bouncing ball with no friction
Yeah similar, just a ball bouncing that bounces off a wall, changes direction, hits another wall, repeats. Sorry im developing for the iPhone
-
- Posts: 71
- Joined: Sat Dec 20, 2008 12:16 pm
- Location: Minnesota
- Contact:
Re: Random bouncing ball with no friction
It might be easier to just create a quick sample project for you than actually explain it, mostly because I'm not understanding why it might be going through. Have you made your steps smaller? What is your spacestep now? Too small of a space step can cause objects to pass through each other.TheServo wrote:Yeah similar, just a ball bouncing that bounces off a wall, changes direction, hits another wall, repeats. Sorry im developing for the iPhone
-
- Posts: 4
- Joined: Wed Jun 10, 2009 12:22 pm
- Contact:
Re: Random bouncing ball with no friction
My step is set to 1/60. I changed it to 1/30, but I'm experiencing the same issues. I must be going about this the wrong way.
-
- Posts: 71
- Joined: Sat Dec 20, 2008 12:16 pm
- Location: Minnesota
- Contact:
Re: Random bouncing ball with no friction
The wrong way is right since 1/30 is a bigger number than 1/60TheServo wrote:My step is set to 1/60. I changed it to 1/30, but I'm experiencing the same issues. I must be going about this the wrong way.
Any chance you want to share your project with me so I could take a look at what may be the problem?
-
- Posts: 4
- Joined: Wed Jun 10, 2009 12:22 pm
- Contact:
Re: Random bouncing ball with no friction
Lol yeah, you're right
my step, and updateBalls. smallBalls is an NSMutableArray of my SmallBall class that wraps cpBody
I setup walls that are the outter dimensions of the iPhone in landscape mode, 480x320, with e->1 and u->0
If there wasn't any physics I would just randomly add the ball and if it hit a wall I would reverse its x,y variables. The physics is making it difficult for me because there are more factors to take into account. So my question is what factors do I need to take into account to keep the SmallBall within the screen but constantly bouncing all around?
Thanks
Code: Select all
- (cpBody *)makeBallX:(float)x y:(float)y {
Sprite *ball = [[Sprite spriteWithFile: @"ball.png"] retain];
[self addChild: ball];
cpBody *ballBody = cpBodyNew(20.0, cpMomentForCircle(20.0f, 10.0f, 10.0f, cpvzero));
ballBody->p = cpv(x,y);
// random velocity for now
ballBody->v = cpv(300,-150);
cpSpaceAddBody(space, ballBody);
cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0f, cpvzero);
ballShape->e = .8;
ballShape->u = .0;
ballShape->data = ball;
ballShape->collision_type = 1;
cpSpaceAddShape(space, ballShape);
return ballBody;
}
Code: Select all
-(void)step: (ccTime)delta {
int steps = 2;
cpFloat dt = delta / (cpFloat)steps;
for(int i = 0; i < steps; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
[self updateSmallBalls];
}
- (void)updateSmallBalls {
for (SmallBall *b in smallBalls) {
if ([b getX] > 460 || [b getX] < 20) {
//in 2d, no physics, pos.x = -pos.x;
}
if ([b getY] > 300 || [b getY] < 20) {
//in 2d, no physics, pos.y = -pos.y;
}
}
}
If there wasn't any physics I would just randomly add the ball and if it hit a wall I would reverse its x,y variables. The physics is making it difficult for me because there are more factors to take into account. So my question is what factors do I need to take into account to keep the SmallBall within the screen but constantly bouncing all around?
Thanks
Who is online
Users browsing this forum: No registered users and 5 guests