|
|
Hi,
I'm not a maths guy, can any of you Gurus help me out here.
I've sussed detecting if two spheres intersect ( distance between centers
less combined radius ) but...
I want to detect if there is any collision between a box and a sphere with a
mathematical formular. Can it be done? How?
sphere is at x1,y1,z1 and of radius n1. Box is from x2,y2,z2 to
x3,y3,z3
Thanks in advance...
Post a reply to this message
|
|
|
|
> I want to detect if there is any collision between a box and a sphere
> with a mathematical formular. Can it be done? How?
>
> sphere is at x1,y1,z1 and of radius n1. Box is from x2,y2,z2
> to
> x3,y3,z3
A box is the intersection of three pairs of axis-aligned planes (where
a plane has an inside an an outside, like in PoV).
Collide the sphere with each plane in turn (this is easy) and apply each
of the following rules in the order given, where "partially inside"
means, for the plane at x3 (assuming x3>x2) x1-n1<x3; "entirely inside"
means x1+n1<x3; "entirely outside" means x1-n1>x3:
If the sphere is entirely outside any plane, they don't collide.
If the sphere is entirely inside all planes, they don't collide (the
sphere is inside the box).
If (x2,y2,z2) is inside the sphere and (x3,y3,z3) is inside the sphere,
they don't collide (the box is inside the sphere).
If the sphere is partially inside two opposite planes, they collide.
You still might get this circumstance, though:
|
-. sphere
\ (almost)
-------+ \
| -_
box | ---
|
where the sphere is partially inside exactly two non-opposite planes
(let's assume its the x-most and y-most planes, and that the point
marked with a + is (x3,y3,z3)). If, in this case, (x3,y3,z1) is inside
the sphere, they collide; if it is outside the sphere, they don't. Adapt
the choice of numbers and which co-ordinate is taken from the sphere to
suit which planes they are.
There is also the possibility for the above case to happen in 3D, where
the sphere is partially inside three planes that meet at a corner - if
this corner is inside the sphere, they collide; if it is outside, they
don't.
Phew.
Turning this into an actual algorithm is left as an exercise for the
reader. :-> You might like to look at the Cohen-Sutherland line-clipping
algorithm, which intersects points with boxes in 2D or 3D and uses
bitmasks and fast comparisons to do similar rules - it could easily be
adapted for this.
I hope this maths-dump is of some use to you.
Daniel
--
Misinformation followed us like a plague You can beat us with wires
Nobody knew from time to time You can beat us with chains
If the plans had changed You can run out your rules but you know
.: www.doublezero.uklinux.net :. You can't outrun the history train
Post a reply to this message
|
|