Hi Patrick,
Hope this is of use to you. This is my CollisionDetector class, see
further down for how to use it.
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;
/**---------------------------------------------------------------------
* Class: CollisionDetector.java
* @author Gareth Powell (c) 1999
* CollisionDetector extends the Java3D Behavior class and allows shapes to
detect
* collisions with one another.
**/
public class CollisionDetector extends Behavior {
WakeupOnCollisionEntry entry; // Collision Entry detector
WakeupOnCollisionExit exit; // Collision Exit detector
Shape3D shape; // The shape
boolean inCollision; // Collision state
/**---------------------------------------------------------------------
* Constructor: CollisionDetector(Shape3D s)
* @param s The shape that will detect collisions
**/
public CollisionDetector(Shape3D s) {
shape = s;
inCollision = false;
} // end CollisionDetecter
/**---------------------------------------------------------------------
* Method: initialize()
* Initializes the Collision detector
**/
public void initialize() {
entry = new WakeupOnCollisionEntry(shape);
exit = new WakeupOnCollisionExit(shape);
wakeupOn(entry);
} // end initialise
/**---------------------------------------------------------------------
* Method: processStimulus(Enumeration criteria)
* This is the code that runs when a collision is detected
* @param
**/
public void processStimulus(Enumeration criteria) {
inCollision = !inCollision;
if (inCollision) {
System.out.println("In Collision");
/**
* XXX Further code handling of Collisions should go here XXX
**/
wakeupOn(exit);
}
else {
System.out.println("NOT in Collision");
wakeupOn(entry);
}
} // end processStimulus
} // end CollisionDetector.java
======================================================================
OK. To use this class, put this code in a TransformGroup or
something...where your shape3d objects are
bounds = new BoundingSphere( new Point3d( 0.0,0.0,0.0 ), 1.1 ); //
These are the bounds for the collision (slightly bigger than my cube)
body.setCollisionBounds(bounds); // set the shape's collision
bounds, body is a Shape3D object (a cube in my case dimensions 1,1,1)
CollisionDetector cd = new CollisionDetector(body); // Create the
collision detector and pass it a reference to your shape3d object
cd.setSchedulingBounds(bounds); // Set scheduling bounds for the
Collision detector, needs to know what bounds to use
this.addChild(cd); // add the detector to the shape3d's parent
TransformGroup
======================================================================
If you don't want some objects in your world to signal collisions then use
these methods to disable the collision detection of certain objects.
void setCollidable(boolean collidable)
Sets the collidable value; determines whether this
node and any of its children, if a group node, can
be considered for collision purposes.
boolean getCollidable()
Returns the collidable value; this value determines
whether this node and it's children, if a group
node, can be considered for collision purposes; if
it is set to false, then neither this node nor any
children nodes will be traversed for collision
purposes; the default value is true.
Thanks to Travis Bryson for pointing that one out to me.
If you would like me to send you a complete source code example, just say.
Again, I hope this is of use.
Gareth.
----- Original Message -----
From: ing. P.W.L. JANSSEN <[EMAIL PROTECTED]>
To: Gareth Powell <[EMAIL PROTECTED]>
Sent: Friday, April 09, 1999 10:42 AM
Subject: Re: [java3d] Collision Detection
> Hi Gareth,
> I read that the collision detection you've made works fine. It's propably
two
> months ago that I quit trying to make a collision detection, because every
time
> when I start up my applet the collision arises and never dissappear. I
have read
> that many other programmers had the same problem. I will appreceate if you
send
> me the relevent code so I can make a collision detection too.
> Much thanks in advance.
>
> Regards Patrick,
>
> Gareth Powell wrote:
>
> > I have 3 visible objects in my world, a Box (primitive and non-moving)
and 2
> > Shape3D boxes (moving). I have written a Behavior class to detect
collisions
> > between the objects and this works fine.
> >
> > The problem is I don't want the moving boxes to detect the collision
with
> > the static, non-moving box. A collision should be signaled when they
> > collide with one another and at no other time.
> >
> > Is there any way of excluding an object from collision detection.
> >
> > Can anybody help?
> >
> > Thanks..
> >
> > Gareth.
> >
> > =====================================================================
> > To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
> > Java 3D Home Page: http://java.sun.com/products/java-media/3D/
>
>
>
>
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/