Java, OpenGL, and Angles

PrisonLove

Hard Realist
Reaction score
78
Okay so I have a couple of questions. First of all I'm working on a little animation. In my animation there is a chubby person and he is constantly moving toward an ice cream cone. There is a monster chasing him. That is the scenario. I am working with 3D points.

I'm using a rotation matrix before drawing the characters to make certain that they always face the object that they're going for (chubby faces ice cream and monster faces chubby). I don't know how to calculate the angles that they should face.

If chubby is at point p1 and ice cream is at point p2, I need chubby to face the angle from p1 to p2. How do I calculate this? The same thing would apply to the monster.

Next is movement. I keep track of the coordinates of every character (including the ice cream) so I figure I would also use a translation matrix to make them move. That makes sense, but I need to know how to calculate the values to translate.

Here is what updating a character would look like in my code so far:

Code:
gl.glPushMatrix();
    gl.glTranslated(chubbyLoc.x, chubbyLoc.y, chubbyLoc.z);
    gl.glRotated(chubbyFacing, 0, 1, 0);
    chubby.draw( gl );
gl.glPopMatrix();

draw( gl ) is how I draw the characters, all characters have working draw methods, and animate properly. They're rotation and position is the focus of this thread.

I have a method to get the angle, but there is an issue. I'll illustrate with chubby and ice cream. When ice cream is in quadrants II or IV, chubby faces the correct location. However, when ice cream is in quadrants I or III, chubby faces the opposite direction. Why is this? This is my code for that function:

Code:
public double angleBetweenPoints(Coord p1, Coord p2) {
    double p1x = p1.x;	double p2x = p2.x;
    double p1y = p1.y;	double p2y = p2.y;  //I am aware these values are unused
    double p1z = p1.z;	double p2z = p2.z;
	   
    double radians = Math.atan2(p2z-p1z, p2x-p1x);
    double degrees = Math.toDegrees(radians);
    return degrees;
}

As for updating their locations, I'm fairly stumped. I figured I would need some sort of polar projection. How do I polar project points in Java?
 

DrEvil

FCRI Associate!
Reaction score
111
atan2 takes the y and x not z and x (I'm sure, because your working out the angle from a 2D surface x & y)

polar projection is simple:

current X/Y + speed (or distance w/e..) *cos/sin(facing)

(maybe cos and sin are stored in some math class for Java? )
 

PrisonLove

Hard Realist
Reaction score
78
I'm using z because y is the up vector in my animation. The characters move on the ground, which is the xz plane. Should I still use y to find the angle? Also should I use z or y for polar projection, since the characters move in the xz plane? What is the polar projection formula for the z coordinate?

How would you project a 3d point in general? Assume that x,y, and z are used.

Remember, this is the coordinate system I'm working in:

*warning poor ASCII drawing incoming!*

Code:
        y
        |
        |
        o-------x
       /
      /
    z

Where the ground is the xz plane and up is in the y direction


Also, for getting the angle I tried this:

Code:
public double angleBetweenPoints(Coord p1, Coord p2) {
    Coord pA = p1.normalize();  Coord pB = p2.normalize();
    double dp = dotProduct(pA, pB);
    return Math.toDegrees(Math.acos(dp));
}

public double dotProduct(Coord p1, Coord p2) {
    return p1.x*p2.x + p1.y*p2.y + p1.z*p2.z;
}


/*From Coord Class*/
public double length() {
    double l = x*x + y*y + z*z;
    return Math.sqrt(l);
}
  
//returns a normalized version of this Coord
public Coord normalize() {
    double length = length();
    double x0 = x/length;
    double y0 = y/length;
    double z0 = z/length;
	  
    return new Coord(x0, y0, z0);
}

**Note: Coord is simply a data structure with 3 double values of x, y, z

But that doesn't seem to work at all. The angle it gives me is all wrong. Any help?
 

PrisonLove

Hard Realist
Reaction score
78
Sorry for the double post, but the Illustration probably best describes what I need.

In short, assume chubby is at point A and ice cream is at point B. I need to find angle delta

I also need to know how to do a polar projection for a 3d point (for all values, x,y, and z).

Here is the illustration:

illustration.jpg



Or, to be more specific, I need the either the counter-clockwise angle from the horizontal axis to point B on the line AB OR I need SIGNED angle between two vectors, both in 3D and 2D.
 
Reaction score
333
Do you actually need to use angles? You can construct your rotation matrix out of orthogonal unit vectors.

  1. Get the look vector (facing direction) by normalizing the vector from the object to the target.
  2. Find an up vector. If the target is never going to be directly above (or below) the object, you can use (0,1,0) as your up vector.
  3. Use the cross product of the up and look vectors to get the right vector.
  4. Now cross the look and right vectors to get the final up vector (we do this to make sure that the up vector is orthogonal to the other two).
Now you can use the right, up, and look vectors as the columns of your rotation matrix:

Code:
[right.x, up.x, look.x
 right.y, up.y, look.y
 right.z, up.z, look.z]

The result of applying this rotation will be that the object faces the target. The look, up and right vectors represent the direction of the Z, Y and X axes in the local coordinate space of the object.

Here is how it might look in pseudocode.

Code:
look = normalize(target.pos - object.pos);
up = vec3(0, 1, 0);
right = cross(up, look);
up = cross(look, right);

rotation = mat3(right, up, look);

If the object and target are both on the xy plane, then you can construct your rotation matrix from the look vector directly:

Code:
[look.z, 0, look.x
 0,      1, 0
-look.x, 0, look.z]
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top