Random bouncing ball with no friction

Official forum for the Chipmunk2D Physics Library.
Post Reply
TheServo
Posts: 4
Joined: Wed Jun 10, 2009 12:22 pm
Contact:

Random bouncing ball with no friction

Post by TheServo »

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
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Random bouncing ball with no friction

Post by Buschmaster »

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
Do you mean something similar to like a brickout-type game but without any bricks or paddles?

What platform are you developing for?
TheServo
Posts: 4
Joined: Wed Jun 10, 2009 12:22 pm
Contact:

Re: Random bouncing ball with no friction

Post by TheServo »

Yeah similar, just a ball bouncing that bounces off a wall, changes direction, hits another wall, repeats. Sorry im developing for the iPhone
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Random bouncing ball with no friction

Post by Buschmaster »

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
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
Posts: 4
Joined: Wed Jun 10, 2009 12:22 pm
Contact:

Re: Random bouncing ball with no friction

Post by TheServo »

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.
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Random bouncing ball with no friction

Post by Buschmaster »

TheServo 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.
The wrong way is right since 1/30 is a bigger number than 1/60 :P

Any chance you want to share your project with me so I could take a look at what may be the problem?
TheServo
Posts: 4
Joined: Wed Jun 10, 2009 12:22 pm
Contact:

Re: Random bouncing ball with no friction

Post by TheServo »

Lol yeah, you're right :)

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;
}
my step, and updateBalls. smallBalls is an NSMutableArray of my SmallBall class that wraps cpBody

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;
		}
	}
}
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
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests