Cube/Plane Collisions

Started by
10 comments, last by Bob Janova 17 years, 1 month ago
I did some searching but couldn't find anything on this. I'm trying to do some basic physics stuff. I've got sphere/sphere collisions done (frictionless, can't be with making them spin), sphere/plane collisions, sphere/cube collisions (with angular velocities applied to the cubes), but now I'm stuck making the cubes bounce of planes correctly. I know I'm applying the wrong forces somewhere. Can anyone tell me where I'm going wrong? Okay, here's what I do, currently: 1) I store the 8 corners of the cube in local space. 2) I load these in and transform them by the cube's rotation matrix. 3) I Test the 8 corners of the cube against the plane to see if any of them are the wrong side, thus making the cube intersect with the plane 4) I translate the point out so that it's touching the plane 5) I apply a linear force to the centre of the cube: - I do this as a sphere/plane collision, simply V(l)' = V(l) - (N.V(l)) * (1 + CoR) * N - N is the plane's Normal, V(l) is the linear velocity, CoR is the coefficient of restitution for the plane. That seems to be ok, but I'm wondering if I'm applying too much, and need to take into account things like how far away the point of intersection is away from the line of centres. Also, should I be paying attention also to linear velocity from rotation (something like r x omega) or something? 6) I calculate the rotational impulse on the corner. I calculate the force applied similarly, assuming it's exactly the same as the moment of force calculated on the plane, which is m*V(l). I then transform that by the transpose of the cube's matrix, to put it into cubespace, and then apply it to the relevent corner of the cube. - This could be completely wrong, I seem to not be able to find anything useful on the internet on this. Maybe I'm looking for the wrong things. Does anyone have any ideas as to what I'm doing wrong? The cube I have just starts spinning uncontrollably.
Advertisement
Your basic method of detecting the collision and where to apply the force seems good.

To calculate the size of the impulse, you need to consider conservation of energy, which is non-trivial due to the rotational effects. The total energy for a 'solid plane' (the plane is not affected by the collision) is, if we are in the plane's local system (i.e. the plane is not moving):
½mv.v + ½(Iw).w
(I think). w (really ) is a vector along the axis of rotation and with magnitude equal to the scalar angular velocity. I is the inertia tensor for a cube, which I can't remember but which you can look up.

After applying an impulse X to one corner r of the cube (an arbitrary impulse in terms of the cube's coordinate system),
mv1 = mv0+X,
Iw1 = Iw0+X×r, and
mv0.v0 + (Iw0).w0 = mv1.v1 + (Iw1).w1

This gives you a set of simultaneous equations you can use to solve for the impulse X, given that you know its direction (it is in the direction of the plane normal).

To simplify it a bit, I think we can work in deltas, so the change in linear kinetic energy is
½m[v0.dv+(v0+dv).dv)]
and that in rotational kinetic energy
½[Iw0.dw+(I(w0+dw)).dw)]
Of course dv=X/m and dw=I-1X×r and the sum of energy deltas should be zero.

Because a cube is symmetric, its inertia tensor can be replaced by a scalar which makes things easier. So
m[v0.dv+(v0+dv).dv)] = -I[w0.dw+(w0+dw).dw)]

=> v0.X+(v0+X/m).X = -w0.X×r+(w0+X×r/I).X×r
=> 2v0.X+X.X/m=2w0.X×r+(X×r).(X×r)/I

You know v0 and w0 and the direction of X, so the first dot product on each side becomes a constant scaled by the size of X (a=v0.n, b=v0.(n×r) where n is the plane normal in cube space).

=> 2a|X|+X.X/m = -2b|X|+(X×r).(X×r)/I

|X| can be cancelled, and (X×r).(X×r)=x²r²sin a=3Rx²sin a (R=half length of cube side; a=angle between impulse direction and vector from impact point to centre of cube) leaving
=> 2a+x/m = -(2b+(3Rx sin a)/I)
... a single equation in terms of x (=|X|) and some constants.

This seems altogether too easy so I've probably cocked something up, but that implies
x = -2(a+b)/(1/m + (3R sin a)/I)

If that works I'll be amazed. There's a chance: for sin a = 0 (impulse through the centre of the cube) x=-2v.n which is correct. I hope I helped.

[Edited by - Bob Janova on March 5, 2007 9:31:58 AM]
So how do I calculate X? You've started by using a completely different syntax to what I was using in a way, so it's slightly confusing. I think I understand what you're saying, but I just waat to be sure. Is the X you're talking about simply the -m(V(l) + V(w))?
Right, I think I've finished editing now :).

X is the impulse you're going to apply to the corner of the cube (and to the plane as well, but we're assuming that the plane is infinite mass in this calculation). You know the direction of X (it's the plane normal) so if you calculate x (the length of X) you can find X, and from there it's easy to find the changes to velocity v and angular velocity w.

Edit(!): I think the answer is a 'yes', except that the impulse isn't necessarily the magnitude you thought it was, hence the odd behaviour.
That's what I thought I was doing. X simply being identical to the force that would be applied onto the plane (yes, the plane is immobile). How would that be split up through linear and angular forces? I thought there'd be some interesting thing about dot producting r (the vector from the centre of the cube to the corner) with the plane's normal to find out how much to apply linearly and how much to apply angularly.

Am I missing something silly here?
It's perhaps more likely that I am :P.

X simply being identical to the force that would be applied onto the plane
Indeed, but you can't know what that is without going through conservation of energy equations, because the impulse has to do linear and angular parts. That's what I've tried to work through in my post, though I might have done it wrong. Once I get off work I might have another look and try to knock up a quick simulation.

The impulse isn't split arbitrarily between linear and angular parts; instead, the impulse exerts a linear force and a couple simultaneously and in a fixed ratio (determined by <n>n×r).
Yes, that nxr or n.r seems to be the thing I'm missing.

but if it's exerting a moment of force (I know it need to be m(V(l) + V(r)).n * n so as it's directly along the plane's normal), so taht should be X, but then going from there seems the tricky thing.
Okay, I got it working by using the following formula:

impulse = -((1+e) * (v(r).n))/((1/m) + (n.((r x n)/I))

e = coefficient of restitution, v(r) is the cube's velocity, r is the contact point - the cube's position, n is the plane's normal, m is the cube's mass, and I is the Inertia Tensor.

This creates a force along the plane's normal.

I can then go:

Vnew = Vold + (impulse * n)/m

and
Wnew = Wold + (impulse * n)/timedelta (not sure why I have to timedelta it, but it works).

This now seems to work fine.


I want to add friction, now, because a cube spinning on the spot looks goofy.

So, I'm reading "Physics For Game Developers", by David M Bourg (any of you got this, it's pretty good, barring a few typos).

It's saying just use that impulse formula and go:

Vnew = Vold + (impulse * n + impulse * CoefficientOfFriction * t)/m
where t = tangent unit vector. This confuses me, as I thought I'd calculated the impulse along the normal, and the friction would work only along the tangent. Am I, again, missing something blindingly obvious?

Thanks.
That's the book I had open to attempt to answer your question ;)
Can you please elaborate on:

"The impulse isn't split arbitrarily between linear and angular parts; instead, the impulse exerts a linear force and a couple simultaneously and in a fixed ratio (determined by <n>n×r)."


I was puzzling over this myself. For example, if a rigid ball strikes a free floating rigid bar at some point off center, how do you determine how much of the reaction pushes the bar back, and how much of it causes the bar to rotate.

Also, does the reaction of the ball vary with how far off center it strikes?

I can see that NxR increases the farther off center the strike is, and that it is zero for a center strike. I'm making a wild guess, but do you divide the reaction like so:

angular reaction fraction = (reaction * NxR) / (NxMaximumR)
linear reaction fraction = 1 - angular reaction fraction

??

This topic is closed to new replies.

Advertisement