Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ball physics on uneven meshes

Started by hotpixel Sep 22, 2008 at 10:07 AM 9 replies 1.2k views
Original Post
hotpixel
hotpixel
Hi everyone, I have got a physics lecturer to help me get a ball rolling around on a plane depending on the angle of rotation of the plane. It works out the x and z velocity based on the plane rotation and a gravity value. The ball's y position is not changed only the x and z positions are. The ball is rotated after translation to the same rotation as the plane in order to make it stick to the plane. What I want to do now is to get the ball to react on uneven meshes. I have pulled the plane mesh apart which has inclines and declines on it. I know the three vertices that make the face directly under the ball and I also know the three points that make the face it was on previously. How would I use this data to make it roll over surfaces of any given mesh? I have tried playing with the data but with no luck, I would appreciate any helpful suggestions. Thanks, Andy
oliii
oliii
depends what you need, and what you have at the moment.

If you want to get down to it, then it will be pretty complicated, involving collision detection (that's the difficult part), and constraining the velocity, acceleration and position with the collision planes.
Everything is better with Metal.
hotpixel
hotpixel
I really didn't see the need to get too in depth with collision detection seeing as I am only working with a sphere and I also always know the radius of the sphere so I only really need the vector position for the sphere plus the radius.

Besides I didn't think collisons got more complicated than deconstructing meshes and working out where objects where in relationship to the nearest face on the mesh.

What I want to know is how I utilise the data. Probably more precisely whether to start working with the y axis of the ball and how; or whether to continue using angles and rotations and how.
luca-deltodesco
luca-deltodesco
it involves a bit of collision detection because a sphere could be colliding with multiple triangles on the mesh; imagine a dip in the mesh which has say 8 different triangles making up the dip's sides, at the bottom of the dip the sphere may be colliding with all 8 triangles
hotpixel
hotpixel
I think for the time I will continue using large faces, keep it simple. Just create some ramps and drops out of faces that are larger than the ball. I think the complexities of that sort of thing are a bit beyond me at the moment.

So can I use the data have from a face (three vertices) and where the ball is on the face in order to calculate the height the ball should be at in order for it to look like the ball is rolling over the face?
oliii
oliii
ok then. Working with vectors will help you a lot.

Basically, if you have a ball on an incline, the ball velocity and acceleration are cosntrained by the plane incline.

it satisfied the inequation (v . n) >= 0
same for acceleration

(a . n) >= 0

similarly, the position on the plane is also constrained. Say your plane is defined by a normal n, and a distance to origin d.

(p . n) > (d + r)

if you consider the equation of motion of a particle

v(t) = v0 + a0 * t
p(t) = p0 + v0 * t + 0.5f * a0 + (t * t)

the particle (p, v, a, r) and the plane (n, d), then you can try

if ((a . n) < 0) a -= (a . n) * n;                   // constrain acceleration to the planev += a * t;                                          // update velocityif((v . n) < 0) v -= (v . n) * n;                    // constrain velocity to the planep += v * t + 0.5 * a * (t*t);                        // update positionif((p . n) < (d + r)) p += n * ((d + r) - (p . n));  // constrain position to the plane
Everything is better with Metal.
hotpixel
hotpixel
Thanks Oliii, that looks really good. I hate to be a novice but would yo mind explaining what some of those variables are...?

The face normal great, know what that is, can work with that.

Point to Origin, sorry origin to the face which would be?

particle (p, v, a, r), I'm guessing they are something to do with the properties of a single point so... p = position, v = velocity, a = angles? r = radius??

And there are a few t's in there which I can't even hazard a guess at but other than that it makes sense. :D

Thanks,
Andy
oliii
oliii
d is the distance of the plane to the origin. Generally speaking, you take any point on the plane and dot product with the plane normal.

That is the actual definition of a plane. Careful though, some literature actually use the opposite to represent 'd' (d = -(point_on_plane . n), for example, representing the plane equation as n.x * x + n.y * y + n.z * z + d = 0).

t = frame time step (for example 1 / 30.0f for 30 frames per second).
n = plane normal
d = (any_point_on_plane . n)
p = position
v = velocity
a = acceleration
r = radius
Everything is better with Metal.
hotpixel
hotpixel
Excellent, thanks Oliii, I think I can work with this now ;)
hotpixel
hotpixel
Hi ya,

I've implemented this code into DX but it doesn't seem to be working, would someone mind have a quick check to see that it's okay;

Quote:

Qx = (float)(zTilt);
Qz = (float)(xTilt);

D3DXMatrixRotationYawPitchRoll(&mRotate, 0, Qx, Qz);
D3DXVec3TransformCoord(&n, &triangle.normal, &mRotate);
D3DXVec3TransformCoord(&pointOnPlane, &triangle.aPosition, &mRotate);

d = D3DXVec3Dot(&pointOnPlane, &n);

if ( D3DXVec3Dot(&a, &n) < 0)
a -= (D3DXVec3Dot(&a, &n)) * n; // constrain acceleration to the plane

v += a * fElapsedTime; // update velocity

if((D3DXVec3Dot(&v, &n)) < 0)
v -= (D3DXVec3Dot(&v, &n)) * n; // constrain velocity to the plane

position += v * fElapsedTime + 0.5 * a * (fElapsedTime*fElapsedTime); // update position

if((D3DXVec3Dot(&position, &n)) < (d + g_fObjectRadius))
position += n * ((d + g_fObjectRadius) - (D3DXVec3Dot(&position, &n))); // constrain position to the plane


'v' and 'a' are declared in the constructor as D3DXVECTOR3(0,0,0).

basically I have an accelerometer plugged into my app, this is where the rotations are coming from and that is what Qx and Qz are (pitch and roll). My logic was that if I transform the normal and a position on the face (which is one of the vertices) it will give me the rotated face and would move my ball accordingly but its not working, the ball jumps about a bit if I start throwing the accelerometer around but nothing that really makes any sense. Could you just have a quick look over it and make sure I haven't made any stupid mistakes.

Thanks,
Andy

[Edited by - hotpixel on September 23, 2008 6:16:01 AM]
oliii
oliii
Quote:
Original post by hotpixel
Hi ya,

I've implemented this code into DX but it doesn't seem to be working, would someone mind have a quick check to see that it's okay;

Quote:

Qx = (float)(zTilt);
Qz = (float)(xTilt);

D3DXMatrixRotationYawPitchRoll(&mRotate, 0, Qx, Qz);
D3DXVec3TransformCoord(&n, &triangle.normal, &mRotate);
D3DXVec3TransformCoord(&pointOnPlane, &triangle.aPosition, &mRotate);

d = D3DXVec3Dot(&pointOnPlane, &n);

if ( D3DXVec3Dot(&a, &n) < 0)
a -= (D3DXVec3Dot(&a, &n)) * n; // constrain acceleration to the plane

v += a * fElapsedTime; // update velocity

if((D3DXVec3Dot(&v, &n)) < 0)
v -= (D3DXVec3Dot(&v, &n)) * n; // constrain velocity to the plane

position += v * fElapsedTime + 0.5 * a * (fElapsedTime*fElapsedTime); // update position

if((D3DXVec3Dot(&position, &n)) < (d + g_fObjectRadius))
position += n * ((d + g_fObjectRadius) - (D3DXVec3Dot(&position, &n))); // constrain position to the plane


'v' and 'a' are declared in the constructor as D3DXVECTOR3(0,0,0).

basically I have an accelerometer plugged into my app, this is where the rotations are coming from and that is what Qx and Qz are (pitch and roll). My logic was that if I transform the normal and a position on the face (which is one of the vertices) it will give me the rotated face and would move my ball accordingly but its not working, the ball jumps about a bit if I start throwing the accelerometer around but nothing that really makes any sense. Could you just have a quick look over it and make sure I haven't made any stupid mistakes.

Thanks,
Andy


I'll do a demo later on
Everything is better with Metal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.