mjanisz1
mjanisz1

Reputation: 1516

Moving model in 3d opengl

I want to move my model on a flat surface (y=0) using keyboard buttons but I have the following problem:

I managed to make him revolve around his Y axis, but now i need to make it move in the direction he is facing. How to detect and calculate the displacement to construct a proper translation matrix?

Here is the code attached because something is not going well...

    this.mouseMv = MatrixMath.lookAt(this.eyeX,this.eyeY,this.eyeZ,this.at,this.up);
    Mat4 translate = MatrixMath.translate(this.position);
    this.mouseMv = this.mouseMv.mul(translate);
    this.mouseMv = this.mouseMv.mul(MatrixMath.rotationX(-90.0f));
    this.mouseMv = this.mouseMv.mul(MatrixMath.translate(this.position);
    this.mouseMv = this.mouseMv.mul(this.mouseAngle);

    modeling.use(gl);
    modeling.setUniformMatrix("model_view", this.mouseMv);
    String part = "models/catbody.sgf";
    Mat4 original = this.mouseMv;
        for (Map.Entry<String, VertexBufferObject> entry : vboHashMap.entrySet()) {
            String key = entry.getKey();
            entry.getValue().bind(gl);
            gl.glDrawArrays(GL3.GL_TRIANGLES, 0, SGFLoader.getNumVertices(key));
        }

So the forward vector is the (8,9,10) vector from my mouseMv matrix. Now to make the model move forward the direction he is facing i should translate the mouseMv by translation matrix with the corresponding forward vector?

Upvotes: 1

Views: 1441

Answers (2)

Kevin
Kevin

Reputation: 2730

If you have the angle in which your model is facing, you can just use a really simple sine and cosine calculation.
pseudo code:
newx = oldx + (cos(angle)*speed);
newz = oldz + (sin(angle)*speed);
Your angle has to be in radians (divide by 180 and multiply by pi if it is in degrees) if you use sin() and cos() in math.h

Upvotes: 1

Michael IV
Michael IV

Reputation: 11506

It's hard to see how you move your object as no source code has been provided. But I suppose you already use matrices for rotation. So you just need to rotate the matrix then translate it along the matrix new direction which is forward vector. Now, the question how to calculate a forward vector I will leave for you to solve as this one is trivial.

But in cases you are still confused here is a clue:

http://devmaster.net/forums/topic/5881-forward-vector/

Upvotes: 0

Related Questions