Hi there,
i just want to show you a few classes. You can use them very easily to make objects see other objects. For example enemys that can see the player. Let me know what you think about that or if there is something similar to that. I think AI is the most difficult part. After seeing that Norman has released a great Navmesh pathfinding api, i thought the next step could be to make enemys see you. Well, my version here is just in proof-of-concept state. Everything seems to work fine but i think there is a lot of space for correction because i’m neither a game developer nor very good in mathematics stuff ^^. Ok just tell me what you think about it:
Well first of all i show you the way the SightControl becomes implemented:
[java]
Vector3f sightPointSize = new Vector3f(0.1f, 0.1f, 0.1f);
playerSightObject = new SightObject(assetManager, “Player”); //This is just a node containing the “sightpoints” the SightControl can see
playerSightObject.addSightPoint(“foot_left”, new Vector3f(sightPointSize), new Vector3f(-0.6f, -2.4f, 0), ColorRGBA.Orange); //every sightpoint becomes added to the SightObject
playerSightObject.addSightPoint(“foot_right”, new Vector3f(sightPointSize), new Vector3f(0.6f, -2.4f, 0), ColorRGBA.Orange); //This SightObject will be later the player. It’s position and rotation becomes synchronized with the camera.
playerSightObject.addSightPoint(“knee_left”, new Vector3f(sightPointSize), new Vector3f(-0.6f, -1f, 0), ColorRGBA.Orange); //The SightControl of an Enemy will be able to “see” this SightObject.
playerSightObject.addSightPoint(“knee_right”, new Vector3f(sightPointSize), new Vector3f(0.6f, -1f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“bodyside_left”, new Vector3f(sightPointSize), new Vector3f(-0.4f, -0.2f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“bodyside_right”, new Vector3f(sightPointSize), new Vector3f(0.4f, -0.2f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“shoulder_left”, new Vector3f(sightPointSize), new Vector3f(-0.8f, 1.2f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“shoulder_right”, new Vector3f(sightPointSize), new Vector3f(0.8f, 1.2f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“torso”, new Vector3f(sightPointSize), new Vector3f(0, 0.8f, 0), ColorRGBA.Orange);
playerSightObject.addSightPoint(“head”, new Vector3f(sightPointSize), new Vector3f(0, 1.8f, 0), ColorRGBA.Orange);
sightControl = new SightControl(assetManager, rootNode); //the debugnode with the visible rays becomes added to this node
sightControl.addSightObject(playerSightObject); //Every SightObject you add can be seen by the SightControl
sightControl.setSightAngle(40f); //Sets the sightangle. If a SightObject is within this angle, the SightControl sees it
sightControl.setEnableDebug(true); //enables visible debug rays
sightControl.setDebugRayLength(4f);
sightControl.setDebugRayCount(30f); //count of the sightangle. Only visible if debug is enabled
Box enemyBoxShape = new Box(Vector3f.ZERO, 1f, 1f, 1f); //Load the enemy that will be able to see you, in this case it’s the evil monkey
Geometry enemy = new Geometry(“Bleed-through color cube”, enemyBoxShape);
Material mat_tls = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat_tls.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));
mat_tls.setColor(“Color”, new ColorRGBA(1f, 0f, 1f, 1f));
enemy.setMaterial(mat_tls);
SightNode enemySight = new SightNode(sightControl, enemy); //The SightNode creates another node in it that contains the enemy’s geometry.
enemySight.setLocalTranslation(0, 0, -10); //It is just a helper. With this you can specify an offset to the point from where the rays come from.
rootNode.attachChild(enemySight); //If you attach the SightControl directly to the geometry the rays come from the center of the enemy’s geometry.
enemySight.setSightOffset(new Vector3f(0, 0, -1.2f)); //So the rays will always detect the enemy’s geometry as closest collision.
//We use an offset to make the rays start directly in front of the enemy’s face (in that case the box)
followCamControl = new SightHelper.FollowCameraController(cam); //This helper synchronizes the playerSightObject with the camera’s location and rotation.
followCamControl.setGunBaseOffset(new Vector3f(0, -1.8f, 0));
playerSightObject.addControl(followCamControl);
[/java]
You can use this in update - loop to check if the SightControl sees something:
[java]
//If at least one SightPoint of the SightObject is seen and the sight-ray does not collide with an other
// geometry, the SightObject is considered to be seen and becomes added to the visible-objects-list. Call this in update-loop.
//Now for example you can move the enemy towards this position for example with the Navmesh pathfinder.
List visibleSightObjects = sightObject.getVisibleSightObjects();
[/java]
The pink box is the enemy with a sightangle of 40 degrees. The orange points are the sightpoints that can be seen by the enemy. If ray between the enemy and the sightpoints are red, the enemy does not see the sightpoint. If it is green, the enemy sees the sightpoint (only if there are no other objects that collides with the ray).
The next step will be to make an enemy hear you. It will be rudimentary as this but i think it will help someone to create some hide-and-seek games like amnesia or slenderman or what ever.
Okay that’s it. Let me know what you think about it and tell me if it’s usefull or crap or if there are better solutions for this.
Thanks!
Daniel