# Need Help with vector math to move sphere on road

This topic is 3472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone help me with the math I need to get a sphere(ball) on a road to move left or right as its rolling forward? Basically I have a sphere on a road and when the player pushes forward it moves forward, but I can't get the math to move the sphere to the left or right when they press move left/right... I am thinking I need the Dot product of the fwd vector and the left or right vector? and then move the ball to that angle?

##### Share on other sites
Cross the forward vector with the road's "up" vector to get a "right" vector, scale this and add it to the sphere's position to offset it to either side of the spline.

You'll need the other reference vector though, the up. Most peoples road splines interpolate this along the path as well, to make the road bank.

My spline object has a GetBasisAtTime(float) which returns a matrix representing the complete coordinate space at that point on the spline. Then it's a simple matter of transforming your sphere's local coordinate (Vector(0,heightAboveRoad,sidewaysOffset,1)) by the splines coordinate frame at that point.

##### Share on other sites
Hello Bzroom, been awhile... I am still a bit confused on the whole idea you have shot my way, but would angular velocity be what I am looking for?

Thanks

##### Share on other sites
So this is physically simulated and not just animated along the spline?

How are you getting it to follow the center of the spline in the first place, post a little psuedo code sniplet.

##### Share on other sites
the angular velocity would be the linear velocity amount along the road surface, over the radius of the ball.

the axisof rotation would be the cross product of the velocity direction, and the road surface normal.

rotation_axis.normalise();

then in openGL or whatever, you can use that in the glRotate() function.

EDIT : could be you need the opposite...

and also, there are probably optimisations you can do as well.

##### Share on other sites
Sorry Bzroom I been busy...

I have a procedurally created road and I have the normals stored for later use, and the centerline is stored also.

I will post in a bit after I move to the computer with this code on it....

##### Share on other sites
I call this to get the players pos

playerPos = track.GetPosition(distance + 10.0f, false); //on centerline

then in the render()
//Draw Player
glPushMatrix();
matrix.Translate(playerPos);
matrix *=rotMatrix;
glMultMatrixf(matrix.matrix);
DrawBall();
glPopMatrix();

What I need is when the player presses left/right on the keyboard move the ball to the left or right and keeps rolling forward.

oliii Thanks for the reply, I tried the code you pasted but I can't get it to work with my code as of yet...

##### Share on other sites
Alright, so lets just be clear though, you're manually animating the object along the road, and not using some kind of physics engine to integrate it's position based on forces/torques and collision?

Either way, given the information you provided you can create a coordinate space at any point along the spline.

You have some point P(t) on the spline, N(t) is the normal at P(t), F(t) is the forward direction F(t) = (P(t+0.0001) - P(t)).Normalized(); and you are looking for R(t) which is the right vector at P(t). R(t) = Cross(F(t), N(t));

Construct a matrix with those columns: Matrix transform(F(t), N(t), R(t), P(t)).

That matrix describes the coordinate system at "t" along the spline. I assume you are incrementing "t" to make your ball "travel" along the road.

Now you want to get him off the origin of that coordinate space that we created.

So what you have currently is equivalent to

Vector ballPos = transform * Vector(0,0,0,1); //ball will be placed on spline

And you want to add some horizontal offset, in the R(t) axis.

float sideOffset = 2;
Vector ballPos = transform * Vector(0, 0, sideoffset, 1); //ball will be placed 2 units to the "right" of the spline.

If you wanted the ball to "jump" up off the spline:
Vector ballpos = transform * Vector(0, ballHeight, sideOffset, 1);

Notice that the X component is always zero, that's because we are only moving in the spline space that we created at a specific "t", we wouldn't want to translate any further forward or backward along the spline without updating "t".

edit: your "distance" is equivalent to my "t", and your track.GetPosition is equivalent to my P(t).

##### Share on other sites
This could also be represented as:
Vector ballPos = N(t) * ballHeight + R(t) * sideOffset + P(t);

Which is what i was kind of hinting at in my original post.

You currently have:
Vector ballPos = P(t);

##### Share on other sites
Bzroom, I will have a look when I get home. I am sorry, I forgot, I am doing this manually, no Physics libs, its just a simple arcade game.

I will let you know what I find later with the code you posted.

Thanks

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 13
• 10
• 12
• 10
• ### Forum Statistics

• Total Topics
633001
• Total Posts
3009821
• ### Who's Online (See full list)

There are no registered users currently online

×