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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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