So I am building a top-down 2d game and right now I have a curious problem with firing arrows.
On a mouseclick, I spawn arrows pretty much like the space shooter tutorial:
Node node = new Node("arrow"); Picture pic = new Picture("arrow"); Texture2D tex = (Texture2D) assetManager.loadTexture("Textures/Arrow.png"); pic.setTexture(assetManager, tex, true); node.attachChild(pic); // adjust picture float width = tex.getImage().getWidth(); float height = tex.getImage().getHeight(); pic.setWidth(width); pic.setHeight(height); pic.move(-width / 2f, -height / 2f, 0); // set location of the node to my archer's location node.setLocalTranslation(archer.getWorldTranslation());
So this works fine. When I load up the game, arrows fire from the right location. It’s after I move the guy that the arrows weirdly offset so that they no longer spawn from the middle of the archer’s location. Testing, however, shows that the location of the spawn node and the archer’s location are the same. So because of this I suspect it’s a camera or graphics issue.
But there shouldn’t be because I’m in parallel projection, right?
cam = app.getCamera(); frustumSize = 220f; float aspect = (float) cam.getWidth() / cam.getHeight(); cam.setFrustum(-100, 100, -aspect * frustumSize, aspect * frustumSize, frustumSize, -frustumSize); //key: near, far, left, right, top, bottom cam.setLocation(new Vector3f(cam.getWidth() / 2, cam.getHeight() / 2, 10f)); cam.setParallelProjection(true); camNode = new CameraNode("Camera Node", cam); //This mode means that camera copies the movements of the target: camNode.setControlDir(ControlDirection.SpatialToCamera); camNode.setLocalTranslation(new Vector3f(0, 0, 10f)); // following rotations are to mimic the guiNode camNode.lookAt(archer.getWorldTranslation(), Vector3f.UNIT_Y); Quaternion q = camNode.getLocalRotation().fromAngleAxis(-FastMath.PI/2, Vector3f.UNIT_Y); camNode.rotate(q); //Attach the camNode to the target: archer.attachChild(camNode);
Some more info if this helps: not only does testing imply that the arrows spawn in the right location, they behave correctly too (i.e. if I move a little bit north, the arrows now appear to spawn too far north. If I then fire southerly, they will travel south and then detach as if they had collided with the edge of the screen despite not having actually reached the bottom of the screen).
Let me know if I need to share any more information. Thanks in advance!