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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top