Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Stick and Ball

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

How can I rotate this stick to point towards the ball? In 3D space, mind you. Even a hint in the right direction (no puns) will be much appreciated. Vectors I have: Stick Position Stick Rotation (in picture it's 0,0,0) Stick Origin Stick Deminsions (x1, y16, z1) Ball Position Vectors I want: New Stick rotation to point end at ball. Thanks for absolutely any help. EDIT: Did you know that if you place Geocities into your browser's high-security area (aka risky), it blocks the adds and pop ups? IE6: Tools -> Internet Options -> Security -> Restricted Sites -> Sites -> Add [edited by - Jiia on June 7, 2004 12:52:41 PM]

#### Share this post

##### Share on other sites
Advertisement
I did not know that

Back to the subject, I''d probaby drop that euler stuff (euler angles).

anyway, to rotate an object towards a target, you can construct a matrix.

the axis of rotation will be the cross product of the current direction of the stick, versus the required direction of the stick (ballpos - stickpos).

the angle of rotation required is the arc cosine of the dot product of the two direction.

phew...

Vector StickDir; // you should supply that
Vector RequiredDir = BallPos - StickPos;
RequiredDir.Normalise();

float cos_angle = StickDir.Dot(RequiredDir);
Vector Axis = StickDir.Cross(RequiredDir);
Axis.Normalise();

Matrix Rotation;
Rotation.FromAxisRotation(Axis, acos(cos_angle));

and the definition of FromAxisRotation() is usually

void Matrix:Vector AxisOfRotation, float AngleOfRotation);

you have a D3DX function to do that, but look on the net for that code, it''s well documented.

I don;t know if that would work, but if you want to stick with euler angles, you can try to convert that matrix into euler angles.

#### Share this post

##### Share on other sites
Awesome! Thanks!

VECTOR Axis, Dir;FLOAT Qa;//Dir = BallPos - StickPos;D3DXVec3Normalize(&Dir, &Dir);//D3DXVec3Cross(&Axis, &StickDir, &Dir);Qa = acosf( D3DXVec3Dot(&StickDir, &Dir) );//MATRIX SRot, SOri, SMov, SScale, SState;MatrixScale(&SScale, StickSize.x, StickSize.y, StickSize.z);MatrixMove(&SOri, 0.0f, -StickSize.y / 2.0f, 0.0f); // just moving stick originMatrixRotateAxis(&SRot, &Axis, Qa);MatrixMove(&SMov, StickPos.x, StickPos.y, StickPos.z);//SState = SScale * SOri * SRot * SMov;GEng.Effect->SetMatrix("WorldMatrix",&SState);Cylinder.FxRender();////MatrixMove(&SState, BallPos.x, BallPos.y, BallPos.z);Effect->SetMatrix("WorldMatrix",&SState);Sphere.FxRender();// Sorry for all the //''s. Trying to preserve lines

What about Quaternions? Know anything about them? Could I do something simular with them, but be able to keep a record of the rotation in something other than a matrix? I need this for bone animation, and I really don''t want to go the matrix per key-frame route.

Thanks again for your excellent help

#### Share this post

##### Share on other sites
Umm, also, is it possible to limit rotations around a certain axis? For example, it's totally possible for the stick to face the ball without ever "twisting". Twisting in the case of my stick is Y rotations, since it faces down. Does it have something to do with the stick's initial direction vector? Or can I cancel out Y rotations on the axis in some manner?

Thanks again!

[edited by - Jiia on June 8, 2004 4:22:12 AM]

#### Share this post

##### Share on other sites
you can do teh same with quaternion, create a quaternion from an axis of rotation and a angle of rotation the same way.

as for a smooth rotating movement, it's simply a matter of limiting the angle of rotation (a delta angle), to whatever the speed you want.

Vector Axis; // axis of rotation
float angle; // angle required

float dt = 2.0f; // reach that angle in 2 second time. could also be the frame time step, or tweak the dt depending on how large the angle is.

float angular_velocity = angle / dt; // angular velocity required
float min_angular_velocity = -PI / 4.0f; //-45 degrees per second
float max_angular_velocity = +PI / 4.0f; //+45 degrees per second

if (angular_velocity < min_angular_velocity) angular_velocity = min_angular_velocity;
if (angular_velocity > max_angular_velocity) angular_velocity = max_angular_velocity;

Quaternion RotationQuat;
RotationQuat.FromAxisRotation(Axis, angular_velocity);

you may have to be careful with the roattion stuff, since it might introduce a 'twist'. for things like moving a cannon towards a target. In that case, you have to preserve some orientation, and it is better to use a polar system instead. calculate the elevation and rotation around the axes of the gun base, and turn the gun towards those angles.

I'd do one rotation at a time, like around the 'up' vector of the gun, then recalculate the elevation requierd from the new gun orientation.

the code is more complicated, but not by much.

[edited by - oliii on June 8, 2004 6:46:52 AM]

#### Share this post

##### Share on other sites
Thanks for all of your help, oliii. Didn''t you help me with my collision detection a while back?

I''m still not sure I understand how to limit rotations in a certain axis. What if an animation keyframe has nothing but a quaternion as a state, and then something external in the world caused the bone to rotate even further? How would I go about making sure the entire process doesn''t twist the bone more than it''s joint limit?

I''m sliding off subject here, but what exactly is the axis part of a quaternion? Picturing an axis and wheel on a car, is the quaternion x,y,z like a direction vector for the axis? If so, which way would it point? Or does it matter?

I''ve tried studying this stuff, but the math equation symbols always throw me off. It''s been way too long since I was in college.

#### Share this post

##### Share on other sites
to turn a axis/angle rotation into a quaternion it''s simply

Quaternion& Quaternion::FromRotation(const Vector& Axis, float angle){	float	a2 = angle * 0.5f;	float	si  = sin(a2);	float	co  = cos(a2);//	RotAxis.Normalise(); // this shouldn''t be necessary!	x = si * RotAxis.x;	y = si * RotAxis.y;	z = si * RotAxis.z;	w = co;      return *this;}

so basically, what you are asking for, is inverse kinematics. I remember there was a post I think a day ago about inverse kinematics (IK). that should answer your question.

When you have the system in place, you can do some really cool things, like force a character to put his foot at the exact floor position (like on stairs), instead of skating in mid air, ect...

#### Share this post

##### Share on other sites
quote:
Original post by oliii
so basically, what you are asking for, is inverse kinematics.

You must have misunderstood me. What I''m trying to do will be USED in inverse kinematics, but really has nothing to do with them.

I''m just wanting to limit a quaternion rotation to certain angles within certain axes. For bones, most should be a cone shaped movement area. Some bones also cannot twist (the wrist).

I''m asking if it''s even possible to limit a certain axis using nothing but a quaternion. In other words, to no longer have the euler coordinates to work with. Do I need to construct another quaternion to act as a limiter, or can I use simple rotation vectors?

Pretty sure rotation vectors are a big no, since it takes so much effort to convert from angle-axis to rotation vectors.

#### Share this post

##### Share on other sites
now you''re talking.

I haven''t come across a method of constraining quaternions directly, but I''ve seen something on the web that''s wetting my appetite.

http://www.cmpevents.com/GDx/a.asp?option=C&V=11&SessID=2131

...speaking of which...
http://www.gdconf.com/archives/2004/baltman_rick.ppt

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011315
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!