# bouncing ball in a cube???

Hi all,

does anybody know how to bounce a ball correctly inside a rotating cube, with the gravity of the ball directed downwards. I just cannot get it to work, the ball is either always out of boundaries or the the gravity of the ball is in the wrong direction. pls help…

Are you talking about the ball tumbling around inside the cube with gravity pulling down toward the bottom of the screen? Also is the rotation of the cube what set’s the ball into motion?
Or do you want to just having the ball bouncing around the cube as it rotates?

Originally posted by soul:
[b]Hi all,

does anybody know how to bounce a ball correctly inside a rotating cube, with the gravity of the ball directed downwards. I just cannot get it to work, the ball is either always out of boundaries or the the gravity of the ball is in the wrong direction. pls help… [/b]

tweak this
vel += gravity // eg (0,-1,0)
vel = vel-((N*(vel.N))*2 // do a bounce

. = dotproduct
N = cubes normal
vel = velocity of the ball
btw only do vel = vel-((N*(vel.N))*2 when the sphere hits the cubes surface (u might need to translate first back along the velocity vector so the sphere aint embedded in the cube first)

thanks for the reply, ya I’m actually referring to the first case where the rotation of the cube sets the ball in motion and during this motion the ball is bouncing inside the cube with its gravity directed towards the bottom of the screen and not anywhere else… any advice on this???

Originally posted by nexusone:
[b]Are you talking about the ball tumbling around inside the cube with gravity pulling down toward the bottom of the screen? Also is the rotation of the cube what set’s the ball into motion?
Or do you want to just having the ball bouncing around the cube as it rotates?

[/b]

The approach I would use for this (since it’s a very specialized problem) would to orient the cube along each major axis and leave it stationary. Then, simply rotate the gravity vector along with the camera around the cube. With each tick, increase the sphere’s x,y,z velocity as per the current direction of gravity. If the sphere should intersect with one of the cube’s sides (an easy test since each face is orthogonal to a major axis), simply invert the corresponding vector component for the sphere by subtracting it from zero. This is a classic trick for simple collision detection and response. If you run into a problem where the sphere begins to bounce out of control, you can easily “dampen” it’s velocity by scaling the vector components by a constant value during each tick.

[This message has been edited by Decimal Dave (edited 04-11-2002).]