use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingSphere method collideWithRay.
private int collideWithRay(Ray ray) {
TempVars vars = TempVars.get();
Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
float a = diff.dot(diff) - (getRadius() * getRadius());
float a1, discr;
if (a <= 0.0) {
// inside sphere
vars.release();
return 1;
}
a1 = ray.direction.dot(diff);
vars.release();
if (a1 >= 0.0) {
return 0;
}
discr = a1 * a1 - a;
if (discr < 0.0) {
return 0;
} else if (discr >= FastMath.ZERO_TOLERANCE) {
return 2;
}
return 1;
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingSphere method collideWithRay.
/*
* (non-Javadoc)
*
* @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray)
*/
private int collideWithRay(Ray ray, CollisionResults results) {
TempVars vars = TempVars.get();
Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
float a = diff.dot(diff) - (getRadius() * getRadius());
float a1, discr, root;
if (a <= 0.0) {
// inside sphere
a1 = ray.direction.dot(diff);
discr = (a1 * a1) - a;
root = FastMath.sqrt(discr);
float distance = root - a1;
Vector3f point = new Vector3f(ray.direction).multLocal(distance).addLocal(ray.origin);
CollisionResult result = new CollisionResult(point, distance);
results.addCollision(result);
vars.release();
return 1;
}
a1 = ray.direction.dot(diff);
vars.release();
if (a1 >= 0.0) {
return 0;
}
discr = a1 * a1 - a;
if (discr < 0.0) {
return 0;
} else if (discr >= FastMath.ZERO_TOLERANCE) {
root = FastMath.sqrt(discr);
float dist = -a1 - root;
Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
results.addCollision(new CollisionResult(point, dist));
dist = -a1 + root;
point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
results.addCollision(new CollisionResult(point, dist));
return 2;
} else {
float dist = -a1;
Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
results.addCollision(new CollisionResult(point, dist));
return 1;
}
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class BoundingSphere method collideWithTri.
private int collideWithTri(Triangle tri, CollisionResults results) {
TempVars tvars = TempVars.get();
try {
// Much of this is based on adaptation from this algorithm:
// http://realtimecollisiondetection.net/blog/?p=103
// ...mostly the stuff about eliminating sqrts wherever
// possible.
// Math is done in center-relative space.
Vector3f a = tri.get1().subtract(center, tvars.vect1);
Vector3f b = tri.get2().subtract(center, tvars.vect2);
Vector3f c = tri.get3().subtract(center, tvars.vect3);
Vector3f ab = b.subtract(a, tvars.vect4);
Vector3f ac = c.subtract(a, tvars.vect5);
// Check the plane... if it doesn't intersect the plane
// then it doesn't intersect the triangle.
Vector3f n = ab.cross(ac, tvars.vect6);
float d = a.dot(n);
float e = n.dot(n);
if (d * d > radius * radius * e) {
// Can't possibly intersect
return 0;
}
// We intersect the verts, or the edges, or the face...
// First check against the face since it's the most
// specific.
// Calculate the barycentric coordinates of the
// sphere center
Vector3f v0 = ac;
Vector3f v1 = ab;
// a was P relative, so p.subtract(a) is just -a
// instead of wasting a vector we'll just negate the
// dot products below... it's all v2 is used for.
Vector3f v2 = a;
float dot00 = v0.dot(v0);
float dot01 = v0.dot(v1);
float dot02 = -v0.dot(v2);
float dot11 = v1.dot(v1);
float dot12 = -v1.dot(v2);
float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
if (u >= 0 && v >= 0 && (u + v) <= 1) {
// We intersect... and we even know where
Vector3f part1 = ac;
Vector3f part2 = ab;
Vector3f p = center.add(a.add(part1.mult(u)).addLocal(part2.mult(v)));
CollisionResult r = new CollisionResult();
Vector3f normal = n.normalize();
// a is center relative, so -a points to center
float dist = -normal.dot(a);
dist = dist - radius;
r.setDistance(dist);
r.setContactNormal(normal);
r.setContactPoint(p);
results.addCollision(r);
return 1;
}
// Check the edges looking for the nearest point
// that is also less than the radius. We don't care
// about points that are farther away than that.
Vector3f nearestPt = null;
float nearestDist = radius * radius;
Vector3f base;
Vector3f edge;
float t;
// Edge AB
base = a;
edge = ab;
t = -edge.dot(base) / edge.dot(edge);
if (t >= 0 && t <= 1) {
Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect8);
// distance squared to origin
float distSq = Q.dot(Q);
if (distSq < nearestDist) {
nearestPt = Q;
nearestDist = distSq;
}
}
// Edge AC
base = a;
edge = ac;
t = -edge.dot(base) / edge.dot(edge);
if (t >= 0 && t <= 1) {
Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect9);
// distance squared to origin
float distSq = Q.dot(Q);
if (distSq < nearestDist) {
nearestPt = Q;
nearestDist = distSq;
}
}
// Edge BC
base = b;
Vector3f bc = c.subtract(b);
edge = bc;
t = -edge.dot(base) / edge.dot(edge);
if (t >= 0 && t <= 1) {
Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect10);
// distance squared to origin
float distSq = Q.dot(Q);
if (distSq < nearestDist) {
nearestPt = Q;
nearestDist = distSq;
}
}
// done.
if (nearestPt != null) {
// We have a hit
float dist = FastMath.sqrt(nearestDist);
Vector3f cn = nearestPt.divide(-dist);
CollisionResult r = new CollisionResult();
r.setDistance(dist - radius);
r.setContactNormal(cn);
r.setContactPoint(nearestPt.add(center));
results.addCollision(r);
return 1;
}
// Finally check each of the triangle corners
// Vert A
base = a;
// distance squared to origin
t = base.dot(base);
if (t < nearestDist) {
nearestDist = t;
nearestPt = base;
}
// Vert B
base = b;
// distance squared to origin
t = base.dot(base);
if (t < nearestDist) {
nearestDist = t;
nearestPt = base;
}
// Vert C
base = c;
// distance squared to origin
t = base.dot(base);
if (t < nearestDist) {
nearestDist = t;
nearestPt = base;
}
if (nearestPt != null) {
// We have a hit
float dist = FastMath.sqrt(nearestDist);
Vector3f cn = nearestPt.divide(-dist);
CollisionResult r = new CollisionResult();
r.setDistance(dist - radius);
r.setContactNormal(cn);
r.setContactPoint(nearestPt.add(center));
results.addCollision(r);
return 1;
}
// Nothing hit... oh, well
return 0;
} finally {
tvars.release();
}
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestBoneRagdoll method simpleInitApp.
public void simpleInitApp() {
initCrossHairs();
initMaterial();
cam.setLocation(new Vector3f(0.26924422f, 6.646658f, 22.265987f));
cam.setRotation(new Quaternion(-2.302544E-4f, 0.99302495f, -0.117888905f, -0.0019395084f));
bulletAppState = new BulletAppState();
bulletAppState.setEnabled(true);
stateManager.attach(bulletAppState);
bullet = new Sphere(32, 32, 1.0f, true, false);
bullet.setTextureMode(TextureMode.Projected);
bulletCollisionShape = new SphereCollisionShape(1.0f);
// bulletAppState.getPhysicsSpace().enableDebug(assetManager);
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
setupLight();
model = (Node) assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
// model.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_X));
//debug view
AnimControl control = model.getControl(AnimControl.class);
SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
Material mat2 = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
mat2.getAdditionalRenderState().setWireframe(true);
mat2.setColor("Color", ColorRGBA.Green);
mat2.getAdditionalRenderState().setDepthTest(false);
skeletonDebug.setMaterial(mat2);
skeletonDebug.setLocalTranslation(model.getLocalTranslation());
//Note: PhysicsRagdollControl is still TODO, constructor will change
ragdoll = new KinematicRagdollControl(0.5f);
setupSinbad(ragdoll);
ragdoll.addCollisionListener(this);
model.addControl(ragdoll);
float eighth_pi = FastMath.PI * 0.125f;
ragdoll.setJointLimit("Waist", eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi);
ragdoll.setJointLimit("Chest", eighth_pi, eighth_pi, 0, 0, eighth_pi, eighth_pi);
//Oto's head is almost rigid
// ragdoll.setJointLimit("head", 0, 0, eighth_pi, -eighth_pi, 0, 0);
getPhysicsSpace().add(ragdoll);
speed = 1.3f;
rootNode.attachChild(model);
// rootNode.attachChild(skeletonDebug);
flyCam.setMoveSpeed(50);
animChannel = control.createChannel();
animChannel.setAnim("Dance");
control.addListener(this);
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("toggle") && isPressed) {
Vector3f v = new Vector3f();
v.set(model.getLocalTranslation());
v.y = 0;
model.setLocalTranslation(v);
Quaternion q = new Quaternion();
float[] angles = new float[3];
model.getLocalRotation().toAngles(angles);
q.fromAngleAxis(angles[1], Vector3f.UNIT_Y);
model.setLocalRotation(q);
if (angles[0] < 0) {
animChannel.setAnim("StandUpBack");
ragdoll.blendToKinematicMode(0.5f);
} else {
animChannel.setAnim("StandUpFront");
ragdoll.blendToKinematicMode(0.5f);
}
}
if (name.equals("bullet+") && isPressed) {
bulletSize += 0.1f;
}
if (name.equals("bullet-") && isPressed) {
bulletSize -= 0.1f;
}
if (name.equals("stop") && isPressed) {
ragdoll.setEnabled(!ragdoll.isEnabled());
ragdoll.setRagdollMode();
}
if (name.equals("shoot") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(matBullet);
bulletg.setLocalTranslation(cam.getLocation());
bulletg.setLocalScale(bulletSize);
bulletCollisionShape = new SphereCollisionShape(bulletSize);
RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, bulletSize * 10);
bulletNode.setCcdMotionThreshold(0.001f);
bulletNode.setLinearVelocity(cam.getDirection().mult(80));
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
}
if (name.equals("boom") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(matBullet);
bulletg.setLocalTranslation(cam.getLocation());
bulletg.setLocalScale(bulletSize);
bulletCollisionShape = new SphereCollisionShape(bulletSize);
BombControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
bulletNode.setForceFactor(8);
bulletNode.setExplosionRadius(20);
bulletNode.setCcdMotionThreshold(0.001f);
bulletNode.setLinearVelocity(cam.getDirection().mult(180));
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
}
}
}, "toggle", "shoot", "stop", "bullet+", "bullet-", "boom");
inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("boom", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("stop", new KeyTrigger(KeyInput.KEY_H));
inputManager.addMapping("bullet-", new KeyTrigger(KeyInput.KEY_COMMA));
inputManager.addMapping("bullet+", new KeyTrigger(KeyInput.KEY_PERIOD));
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestBrickTower method simpleInitApp.
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
// bulletAppState.setEnabled(false);
stateManager.attach(bulletAppState);
bullet = new Sphere(32, 32, 0.4f, true, false);
bullet.setTextureMode(TextureMode.Projected);
bulletCollisionShape = new SphereCollisionShape(0.4f);
brick = new Box(brickWidth, brickHeight, brickDepth);
brick.scaleTextureCoordinates(new Vector2f(1f, .5f));
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);
initMaterial();
initTower();
initFloor();
initCrossHairs();
this.cam.setLocation(new Vector3f(0, 25f, 8f));
cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));
cam.setFrustumFar(80);
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "shoot");
rootNode.setShadowMode(ShadowMode.Off);
bsr = new PssmShadowRenderer(assetManager, 1024, 2);
bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
bsr.setLambda(0.55f);
bsr.setShadowIntensity(0.6f);
bsr.setCompareMode(CompareMode.Hardware);
bsr.setFilterMode(FilterMode.PCF4);
viewPort.addProcessor(bsr);
}
Aggregations