URL87
URL87

Reputation: 11012

How to change coordinate regularly using gl.glTranslatef() ?

Having the following display() -

float tranX , tranY , tranZ ; 
public void display(GLAutoDrawable gLDrawable) {
    final GL gl = gLDrawable.getGL();
    gl.glLoadIdentity();
    gl.glPushMatrix();
    gl.glTranslatef(tranX ,tranY ,tranZ);
    gl.glCallList(i);
    gl.glPopMatrix();
    tranX += 0.05;
    tranY += 0.05;
    tranZ += 0.05;
}

As you can see each display() calling the matrix of the object in gl.glCallList(i) saved and get coordinates change by gl.glTranslatef(tranX ,tranY ,tranZ) .

Suppose that at some stage I want to save this object at its current position (after gl.glTranslatef calling ) and start to translate it to another tranX , tranY , tranZ values .

How could I save this object position considering the above gl.glPushMatrix() and gl.glPopMatrix() ?

Upvotes: 0

Views: 475

Answers (2)

Surveon
Surveon

Reputation: 723

Assuming you're translating from the origin (and even if you're not) - it should be quite possible to save the position of the object relative to the origin in this case. You might use an object that stores the data in three fields (xPosition, yPosition, zPosition).

To translate the object later on, you would first translate to this position and then translate from there as needed.

Upvotes: 1

jozxyqk
jozxyqk

Reputation: 17256

Push/pop matrices are there to accumulate complex matrix transformations that would otherwise be painful to do by hand. For storing and moving object positions, keeping variables as you have done is correct. To expand on that and, as you say start moving in another, add a directionX/y/Z. Eg, tranX += directionX etc. Then when you want to change direction, simply set directionX/Y/Z to a different value.

The speed will change depending on how fast your computer is though. You'll want to find the time since the last frame (or last call to display) and do something like this: transX += velocityX * deltaTime etc.

If you want to move an object from one point to another specific point, you want to look into key-framed interpolation. For example position = pointA * (1.0 - x) + pointB * x and make x move from 0 to 1 (x += speed * deltaTime). When x is above one, pointA becomes pointB and pointB is set to the next position in the list. Then subtract 1.0 from x and continue.

Upvotes: 1

Related Questions