0
votes

So originally I wanted my little 'ship' to have turrets that track a target. This is the jfiddle for it. http://jsfiddle.net/czGZF/2/ When they track, they act odd. I noticed that it thinks that the turret is slighly next to it (at the origin), by this peice of code.

turret_a.position.y = .25;
turret_a.position.z = 2;

However, I had done that so it could be a relative position for when i called (below) to add it to the 'base ship'

ship = new THREE.Object3D();
ship.add( ship_base );
ship.add( turret_a ) ;

When i changed the position of turret_a after it had been added to the ship, and after the ship was added to the scene, the turret tracked mostly how i wanted it to look.

I guess my question is, Why is the lookAt() function using its old location of that, and not the location of where it currently is on its parent object to determine the rotation angles that it needs to be at?

2
Uhmm... Did you read my answer?WestLangley
Yes, and I know that it works swell when the ship is at the origin, but I want the ship to be able to move away from the origin and have its children be able to use lookAt correctly.Lutcikaur

2 Answers

0
votes

If you look at the code for Object3D.lookA(), you will see

// This routine does not support objects with rotated and/or translated parent(s)

Your code works if the parent ship is located at the origin and is not rotated.

Updated fiddle: http://jsfiddle.net/czGZF/4/

three.js r.59

0
votes

From the API, the lookAt() method for Camera objects is defined to use world position, as you've discovered. This seems to be relatively common way to handle things.

I'm not that familiar with the three.js API in particular, but it appears that if you want to get the global position of ball, you can use the following:

var targetPos = ball.position.clone();
ball.localToWorld(targetPos);

Hopefully that gets you closer to your goal. Unfortunately, the fiddle you have seems to be (very) non-deterministic, so I can't quickly get a 100% solution for you.