First of all: Sorry, my English isn't quiet good, I'm very new in 3D and I'am using JMonkeyEngine.
What I would like to do?:
A 3d pong game and I want to control the paddle with the mousecursor and/or touchscreen.
What I have done so far:
placed some boxes as floor and walls and the paddle with a coloured material(not included in the example code), put them in a geometry and then in a node an attach it to the root node to make it visible.
Box a = new Box(Vector3f.ZERO, 2, 0.01F, 5); Box b = new Box(Vector3f.ZERO, 2,0.01F, 5); Box c = new Box(Vector3f.ZERO, 0.01F, 1F, 5); Box d = new Box(Vector3f.ZERO, 0.01F, 1F, 5); Box e = new Box(Vector3f.ZERO, 4, 2, 0.01F); Box paddle1 = new Box(Vector3f.ZERO,0.25F,0.15F,0.02F); geomPaddle1 = new Geometry("paddle1", paddle1); Geometry geomBottom = new Geometry("bottom", a); Geometry geomTop = new Geometry("top", b); Geometry geomLeft = new Geometry("left", c); Geometry geomRight = new Geometry("right", d); Geometry geomWall = new Geometry("Wall", e); geomBottom.move(0, 0, 0); geomTop.move(0, 2, 0); geomLeft.move(-2,1,0); geomRight.move(2,1,0); geomWall.move(0,0,-5); geomPaddle1.move(0,1,5); nodePaddle1 = new Node("PaddleNode"); nodePaddle1.attachChild(geomPaddle1); Node walls= new Node("walls"); walls.attachChild(geomBottom); walls.attachChild(geomTop); walls.attachChild(geomLeft); walls.attachChild(geomRight); walls.attachChild(geomWall); rootNode.attachChild(nodePaddle1); rootNode.attachChild(walls);
The camera is set in z-axis "-1" direction in front of the paddle and the flying cam is disabled
cam.setLocation(new Vector3f(0,1,8)); flyCam.setEnabled(false);
So, where is my problem?
I would like to attach the middle of the paddle to the mousecursor(mouseposition). I have read around and I think I have to translate the 2d mousecoordinates to 3d worldcoordinates. I found some code to do that:
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Then I thought I only have to adjust or fix the z-axis in the simpleUpdate loop, because I only want the x,y-movement.
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0f); Vector3f dir2 = new Vector3f(origin.getX(), origin.getY(), 5F); geomPaddle1.setLocalTranslation(dir2);
but it doesn't worked as I thought:
The paddle move in the correct direction but not to the mousecoordinates.
Well.. I'm very new at this things, I dont know wheter I'am completely wrong or how I can implement the mousecontrol in another way. It would be wonderful if you can help me.