Okay, so after working some more on my code, I think I actually know less than I did before…
I’m using a custom control, and I’ve run into two problems:
1.) My understanding of how to get the mouse (x,y) coordinates is way off. I’ve found several different methods for getting the direction the bullet should travel, none of which appear to actually work for me. They are:
[java] // Method 1
Vector2f click2d = inputManager.getCursorPosition();
System.out.println("click2d = " + click2d);
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
System.out.println(“click3d = " + click3d);
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).subtractLocal(click3d).normalizeLocal();
System.out.println(“dir = " + dir);
System.out.println(””);
// Method 2
Vector2f mouse = inputManager.getCursorPosition().normalizeLocal();
System.out.println("mouse = " + mouse);
Vector2f origin = new Vector2f(armModel.getWorldTranslation().x + .5f, armModel.getWorldTranslation().y);
System.out.println("origin = " + origin);
Vector2f target = mouse.subtract(origin).normalize();
System.out.println("target = " + target);
Vector3f dir2 = new Vector3f(target.x, target.y, 0);
System.out.println("dir2 = " + dir2);
System.out.println("");
float distance1 = origin.distance(mouse);
System.out.println("Distance from origin to mouse: " + distance1);
//Method 3
Vector3f loc = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
System.out.println("loc = " + loc);
Vector3f dir3 = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
System.out.println("dir3 = " + dir3);
dir3.subtractLocal(loc).normalizeLocal();
System.out.println("dir3 now = " + dir3);
System.out.println();
// Mehtod 4
Vector2f m = inputManager.getCursorPosition();
System.out.println("m = " + m);
Vector3f playerPos = player.getLocalTranslation();
System.out.println("playerPos = " + playerPos);
Vector3f dif = new Vector3f(m.x-playerPos.x,m.y-playerPos.y,0);
System.out.println("dif = " + dif);
// Method 5 -- uses origin from Method 2.
Vector3f loc2 = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0);
Vector3f path = new Vector3f(loc2.x-origin.x,loc2.y-origin.y,0);
System.out.println("path = " + path);[/java]
Having the player stand at (0,1,0) and target (mouse click) at (6,1,0), I get the following results:
Method 1
click2d = (543.0, 265.0)
click3d = (0.3848734, 0.6931473, 14.0)
dir = (0.0, 0.0, 0.0)
Method 2
mouse = (0.89868885, 0.43858665)
origin = (0.5, 0.65)
target = (0.88347363, -0.46848089)
dir2 = (0.88347363, -0.46848089, 0.0)
Distance from origin to mouse: 0.4512742
Method 3
loc = (-0.5507337, 0.23654345, 14.0)
dir3 = (-0.7864253, 0.059600804, 13.572041)
dir3 now = (-0.45358157, -0.34052092, -0.8235954)
Method 4
m = (0.89868885, 0.43858665)
playerPos = (0.0, 0.65, 0.0)
dif = (0.89868885, -0.21141332, 0.0)
Method 5
path = (-1.0507337, -0.41345653, 0.0)
None of these actually send the bullet in the right direction, nor do they seem to get the actual world coordinates of where the mouse was clicked (unless I’m missing something).
Can anyone tell me which (if any) of these are actually correct? Is there some conversion I’m not using to get the actual world coordinates of part of the screen I clicked on?
Also, my second problem is:
2.) At the moment, the controlUpdate() method for my control consists of:
[java] @Override
protected void controlUpdate(float tpf)
{
if(spatial != null)
{
//System.out.println(“moving to :” + dir);
spatial.move(dir.multLocal(speed*tpf));
}
//System.out.println("x = " + spatial.getWorldTranslation().x + " y = " + spatial.getWorldTranslation().y + " z = " + spatial.getWorldTranslation().z);
}[/java]
Is this incorrect? Because when I attempt to fire a bullet, the bullet will appear at a random point (usually at the origin), but then moves slightly in a straight line away from the player whenever I click the mouse again.
Once again, any help is appreciated.