I am doing a project that needs to have a lot of people (Agent Objects) in the screen and they need to move towards an objective.
For creating a new Agent Object I use:
bot1 = new Agent(x,y,z,assetManager, rootNode, bulletAppState, navMesh);
Where x,y,z are inicial position. assetManaget, rootNode, bulletAppState need to be inside the construtor, that way I can import model to world:
playerNode = (Node) a.loadModel(“Models/Agent.j3o”);
world.attachChild(playerNode); //load model to world
-
I think this is not the best way to do it, because I will need to import model each time I create an object, but after trying to declare playerNode inside main and put it in Agent constructor, it only shows the first Agent model, every others are invisible. Is like I can’t have a different instance objects of the same variable
-
This happens too with the NavMeshPathfinder variable, if I create a NavMesh in main and put it in the Agent construtor, and make a NavMeshFinder inside the Agent class, after use the .ComputePath() function in one Agent, all others agents will have the same path, dont know why.
-
Lastly in terms of perfomance and frames, I think call the class method update, inside the SimpleUpdate() is not a good idea, right? What is the best I can do?
Edit:
Agent Code:
public class Agent extends SimpleApplication implements AnimEventListener{
//agent variables
double posX, posY, posZ;
double direction;
double vel;//nodes for load models Node world; Node playerNode; //animation variables AnimControl controlAnim; AnimChannel channelAnim; //better control BetterCharacterControl agentControl; BulletAppState bullet; //Pathfinder NavMeshPathfinder navi; Agent(double x, double y, double z, double d, AssetManager a, Node n, BulletAppState b, NavMesh navM) { posX = x; posY = y; posZ = z; direction = d; vel = 0; //for now //load model playerNode = (Node) a.loadModel("Models/Agent.j3o"); playerNode.move( (float) x, (float) y, (float) z); world = n; //load animations controlAnim = playerNode.getChild("Cube.001").getControl(AnimControl.class); controlAnim.addListener(this); channelAnim = controlAnim.createChannel(); //maybe need to inicialize on stand channelAnim.setAnim("Walk"); //or "Stand" agentControl = new BetterCharacterControl(0.5f, 1.8f, 1); //radius , heiht, mass playerNode.addControl(agentControl); // set basic physical properties: //agentControl.setJumpForce(new Vector3f(0,5f,0)); agentControl.setGravity(new Vector3f(0,1,0)); agentControl.warp(new Vector3f((float)x, (float)y, (float)z)); // warp character into landscape at particular location // add to physics state bullet = b; bullet.getPhysicsSpace().add(agentControl); bullet.getPhysicsSpace().addAll(playerNode); world.attachChild(playerNode); //load model to world //pathfinder navi = new NavMeshPathfinder(navM); navi.setEntityRadius(0.5f); //agentControl.setCcdMotionThreshold(0.15f); navi.setPosition(new Vector3f((float)x, (float)y, (float)z)); navi.computePath(new Vector3f(0f,0f,0f)); } public void update(float tpf) { //update position based on navmesh and others agents navi.setPosition(playerNode.getWorldTranslation()); //System.out.println(navi.getDirectionToWaypoint()); if(navi.getDistanceToWaypoint() < 0.25f) //TODO change maybe { if(!navi.isAtGoalWaypoint()) //if it is not at last waypoint { navi.goToNextWaypoint(); } else { System.out.print("End"); } } agentControl.setWalkDirection(navi.getDirectionToWaypoint().mult(5)); //time per frame times 1m/s -> 3,6 km/h }
Main:
public void simpleInitApp() {
//load building plant //Spatial plant = assetManager.loadModel("Models/Ed7/Ed7.j3o"); Node plant = (Node) assetManager.loadModel("Models/EdPlaceHolder/Ed.PlaceHolder.j3o"); rootNode.attachChild(plant); //plant.scale(4f,4f,4f); //Load world physics bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); CollisionShape plantShape = CollisionShapeFactory.createMeshShape(plant); landscape = new RigidBodyControl(plantShape, 0); plant.addControl(landscape); bulletAppState.getPhysicsSpace().add(landscape); //Create pathfinder Geometry navGeom = (Geometry) plant.getChild("Navmesh1"); //load navmesh inside plant object directory Mesh mesh = navGeom.getMesh(); NavMesh navMesh = new NavMesh(mesh); //create bots (testing for now) bot1 = new Agent(20,30,75,1,assetManager, rootNode, bulletAppState, navMesh); bot2 = new Agent(52,30,120,1,assetManager, rootNode, bulletAppState, navMesh); } @Override public void simpleUpdate(float tpf) { bot1.update(tpf); bot2.update(tpf); }
Thank you all!!