pointAt

anyone got a pointAt bit of code? as in rotate object A to pointAt object B.

topsssss!!!

i made a pointAt in gl back in the days like this. not sure
if anyone want it :wink:

Here it is:

// returns the distance between two 3d points in space.
public static float getDistance(Vector4 pnt1, Vector4 pnt2) {
double dx = pnt2.x - pnt1.x;
double dy = pnt2.y - pnt1.y;
double dz = pnt2.z - pnt1.z;
return((float)Math.sqrt(dxdx + dydy + dz*dz));
}

// convert x, y location to an angle - azimuth
public static float getAzimuth(Vector4 pnt1, Vector4 pnt2) {
float x = -(pnt1.x - pnt2.x);
float z = -(pnt1.z - pnt2.z);

float rangle = (float)(Math.atan2(x, z));

return(180.0f * rangle / 3.14159f);

}

// convert x, y, z location to an angle - elevation
public static float getElevation(Vector4 pnt1, Vector4 pnt2) {
float y = (pnt1.y - pnt2.y);
float x = (pnt1.x - pnt2.x);
float z = (pnt1.z - pnt2.z);

float d = (float)Math.sqrt(z*z + x*x);
if(d == 0) d = 0.0001f; // just in case we get a zero distance
float ang = (float)Math.atan(y / d);

return((float)Math.toDegrees(ang));

}

If u want the gl bit:

  Vector4 v1 = new Vector4(0f, 0f, 0f);
  Vector4 v2 = new Vector4(10f, 5f, -20); // whatever.

  float azimuth = Utils.getAzimuth(v1, v2);
  float elevation = Utils.getElevation(v1, v2);
  float distance = Utils.getDistanceZX(v1, v2);

  gl.glRotatef(azimuth, 0f, 1f, 0f);
  gl.glRotatef(elevation, 1f, 0f, 0f);

would be cool to have one in Xith3d :slight_smile: part of transform3d or transformGroup maybe?

doesn’t xith3d already have this?

i couldnt find it. anyone no where it is?

Transform3D.lookAt?

Will.

you have to give that eye, center, up…

mine; u give it a vector loaction of the pointing object and the location vector of the object ot point at- returns the angles to rotate. simpler. bish bosh…

“vector loaction of the pointing object” - the ‘eye’
“the location vector of the object ot point at” - the ‘center of view’
Vector pointing up, usually (0,1,0) for Y is up.

I don’t see the difference ???

Will.