Hi there!
I am making a game which involves a gun which shoots Rays (linearly). I am trying to make it so that when a Ray collides with a certain ‘collection’ (group) of objects, it will create some particles on the ground which look like a bullet has hit there.
To do this, I was looking at the TestMousePick.java example, and noticed that they use a Node to group spatials together. So, I created a node called groundShootables which I attach to the rootNode. So far, to test it, I have attached the scene and a rigid body cube to the groundShootables.
The Problem Is:
Shooting the rigid body cube works fine - particles appear as intended. However, shooting the scene (terrain created in the terrain editor - Spatial) doesn’t work. The ray (indicated by the red line which is drawn in the “void shoot(…)” code below) goes straight through the ground without recording any collision at all.
I’m not entirely sure what I have done wrong! I would appreciate some help :lol: !
The “Shoot” Method:
[java]
@Override
public void shoot(float tpf, Vector3f origin, Vector3f direction)
{
time += tpf;
if(emittingParticles == false){
muzzleEmitter.setParticlesPerSec(15);
shellsEmitter.setParticlesPerSec(5);
emittingParticles = true;
}
//Shoot a round every 0.2 seconds.
if(time >= 0.2f)
{
System.out.println("Shooting!");
Ray ray = new Ray(origin, direction);
Line line = new Line(origin, direction); //FOR VISUALZING THE RAY
Geometry rayLine = new Geometry("ray",line); //FOR TESTING
Material lineMat = new Material(aM, "Common/MatDefs/Misc/Unshaded.j3md");
lineMat.setColor("Color", ColorRGBA.Red);
rayLine.setMaterial(lineMat);
groundShootables.attachChild(rayLine);
CollisionResults terrainResults = new CollisionResults();
int numCollisions = groundShootables.collideWith(ray , terrainResults);
if (numCollisions > 0)
{
CollisionResult hit = terrainResults.getClosestCollision();
System.out.println("Hit the ground!");
//Create ground hit particle effect
groundHit.setLocalTranslation(terrainResults.getClosestCollision().getContactPoint());
groundShootables.getParent().attachChild(groundHit);
groundHit.emitAllParticles();
fxTime = 0;
}
time = 0;
}
}
[/java]
Code where the rigid body box is created and attached to the groundShootables Node
[java]
private void initBox()
{
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
geom.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
geom.setLocalTranslation(0, 35, 0);
RigidBodyControl boxPC = (new RigidBodyControl(2f));
geom.addControl(boxPC);
bulletAppState.getPhysicsSpace().add(geom);
groundShootables.attachChild(geom);
//rootNode.attachChild(geom);
}
[/java]
Code where the scene is attached to the groundShootables Node
[java]
private void initScene()
{
sceneModel = assetManager.loadModel("Scenes/new_mexico_desert.j3o");
sceneModel.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
//sceneNode.attachChild(sceneModel); //Commenting/uncommenting this makes no difference
sceneNode.attachChild(SkyFactory.createSky(assetManager, assetManager.loadTexture("Textures/E.png"), assetManager.loadTexture("Textures/W.png"), assetManager.loadTexture("Textures/N.png"), assetManager.loadTexture("Textures/S.png"), assetManager.loadTexture("Textures/T.png"), assetManager.loadTexture("Textures/B.png")));
sceneRigidBody = new RigidBodyControl(0.0f);
sceneModel.addControl(sceneRigidBody);
bulletAppState.getPhysicsSpace().add(sceneRigidBody);
groundShootables.attachChild(sceneModel);
rootNode.attachChild(groundShootables);
rootNode.attachChild(sceneNode);
}