View Full Version : Sphere rotation - rolling effect?

detzel

01-22-2002, 07:11 AM

There have been a couple post on here that try to describe how to make a sphere have a rolling effect but I can't seem to put it together. I want to be able to just move the ball in any direction and have the sphere appear to be rolling in the direction as well. Some sample code would be appreciated, theres something with the math I just can't get.

Why not keep a local matrix of the balls orientation and use the matrix forward vector to move the ball. Then the rolling axis is its right vector.

Just an idea.

In an GLfloat matrix[16] you get the orientation vectors like this:

// Forward vector

x = matrix[8];

y = matrix[9];

z = matrix[10];

// Right vector

x = -matrix[0];

y = -matrix[1];

z = -matrix[2];

// Up vector

x = matrix[4];

y = matrix[5];

z = matrix[6];

// Position (of ball)

x = matrix[12];

y = matrix[13];

z = matrix[14];

Do all you transformations on this matrix.

Then multiply that matrix into gl modelview matrix using

glMultMatrixf(...)

detzel

01-22-2002, 07:55 AM

I found an example that does this and it works sometimes but most of the time the rotation seems messed up. Like the ball will be moving up and it will be turn sideways which gives it the appearence of sliding because its not rolling in the direction on the movement.

chowe6685

01-22-2002, 08:20 AM

Search on google for "quaternions", these offer a solution for your problem

Boris008

01-22-2002, 08:46 AM

What about keeping the ball still and moving a texture on the surface by applying transformations to the texture matrix instead of the model. And I agree, quaterions will help out alot.

Try updating your matrix by the relative change in angles instead of applying the full angles every time.

// Globals

GLfloat roll_angle;

GLfloat old_roll_angle;

// In your roll update code.

GLfloat rel_roll_angle;

rel_roll_angle = roll_angle - old_roll_angle;

old_roll_angle = roll_angle;

// Update matrix

vector3 right;

right = ballmatrix.GetRightVector();

ballmatrix.Rotatef(rel_roll_angle,right);

And always keep moving in ballmatrix->GetForwardVector() direction.

And always update your ball position by

translating your ballmatrix.

And dont LoadIdentity() on your ballmatrix.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.