Hi,
I am trying to develop a line of sight for the enemy and am thinking of using 5 rays - 1 from the center of enemy 2 from right at 1-2 unit distance and similarly 2 on the left. If the first result for any ray is the player then enemy can follow certain AI actions. I have just started with one ray and was just looking at how it detects .3ds model that enemies and players use.
When I fire the ray from the enemy's location, the first 3 objects it detects are itself - object2##0, object3##0, object1##0(they are located at distance 0.0 so I think its from its own .3ds model) and then the fourth one is the closest object. object2##0, object3##0 and object1##0 are string names achieved using .getName() while the parent nodes are object2 (com.jme.scene.Node), object3 (com.jme.scene.Node) and object1 (com.jme.scene.Node) instead of the normal playerNode achieved using .getParent() (see methods below). The .3ds objects are imported using the standard way and the picking is done fairly standard way as well. How can I get the methods to return its actual playerNode so if it gets detected then I can set some actions for the AI.
importing .3ds
private void createGuard()
{
Node model = null;
try
{
MaxToJme C1 = new MaxToJme();
ByteArrayOutputStream BO = new ByteArrayOutputStream();
URL maxFile = Ares.class.getClassLoader().getResource("jmetest/data/model/robot.3ds");
C1.convert(new BufferedInputStream(maxFile.openStream()),BO);
model = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
model.setModelBound(new BoundingBox());
model.updateModelBound();
//scale it to be much bigger than it is originally
model.setLocalScale(1.5f);
//Rotating the model 90 degree along the Y-axis as .3ds models are
//not always straight when imported
Vector3f axis = new Vector3f(Vector3f.UNIT_Y); // this equals (0, 1, 0) and does not require to create a new object
float angle = 1.57f;
model.getLocalRotation().fromAngleAxis(angle, axis);
guardNode = new Node("Guard");
guardNode.attachChild(model);
guardNode.setLocalTranslation(new Vector3f(-60, 14, -10));
scene.attachChild(guardNode);
scene.updateGeometricState(0, true);
}
picking method
private void lineOfSight()
{
Ray ray = new Ray(guardNode.getLocalTranslation(), guardNode.getLocalRotation().getRotationColumn(2));
PickResults results = new BoundingPickResults();
results.setCheckDistance(true);
scene.findPick(ray,results);
if(results.getNumber() > 0)
{
for(int i = 0; i < 4 /*results.getNumber()*/; i++)
{
//System.out.println("Name of the object: " + results.getPickData(i).getTargetMesh().getParentGeom().getName() + " Distance: " + results.getPickData(i).getDistance());
System.out.println(results.getPickData(3).getTargetMesh().getParentGeom().getName());
if(results.getPickData(3).getTargetMesh().getParentGeom().getParent() == playerNode) //fourth result used as first
{ //three are itself as explained
shootAtPlayer(); //in the post
System.out.println("!!!!!!!!!!!!!!!!!Player spotted!!!!!!!!!!!!");
}
}
}
//return true;
}