Adding a rotation function for Item() objects
by Darren Horton · in Torque Game Engine · 06/29/2003 (5:41 pm) · 11 replies
Hi All,
I would like to add a simple Rotate() function to a class I've based based on the Item() class, and I'm a bit stuck.
I'm not too hot with 3D math I'm afraid, so I tried to figure out where/how the default 'spinning item' rotation was applied. I found a couple of references in the source, things like 'assume rotation is on Z axis only', but I just couldn't figure out what it was doing :-(
I have successfully moved objects about in the scene, and would like to be able to store an objects rotation for each axis, in a Point3F/VectorF for example. This is so it can be applied each frame, and deteriorated steadily - I'm trying to model a ball to attempt some physics code :-)
To move the objects, I'm using getTransform(), transform->setColumn(3, thePosition), and setTransform().
Could anyone help me please?
EDIT: I should have mentioned, this is in C++ code - my classes are copies of Item.cc and Item.h with everything renamed accordingly (to Ball).
Thanks in advance,
]DaZMaN[ YM Studios
I would like to add a simple Rotate() function to a class I've based based on the Item() class, and I'm a bit stuck.
I'm not too hot with 3D math I'm afraid, so I tried to figure out where/how the default 'spinning item' rotation was applied. I found a couple of references in the source, things like 'assume rotation is on Z axis only', but I just couldn't figure out what it was doing :-(
I have successfully moved objects about in the scene, and would like to be able to store an objects rotation for each axis, in a Point3F/VectorF for example. This is so it can be applied each frame, and deteriorated steadily - I'm trying to model a ball to attempt some physics code :-)
To move the objects, I'm using getTransform(), transform->setColumn(3, thePosition), and setTransform().
Could anyone help me please?
EDIT: I should have mentioned, this is in C++ code - my classes are copies of Item.cc and Item.h with everything renamed accordingly (to Ball).
Thanks in advance,
]DaZMaN[ YM Studios
About the author
#2
I recommend using the EulerF deal combined with matrices... i.e.
MatrixF mat(EulerF(mDegToRad(45),0,0));
It seems like you know the rest. Oh, contruct the mat matrix as above, then after that do the setColumn(3, position) thing as before, then apply the setTransform for best results.
06/29/2003 (11:57 pm)
Yeah, it's actually columns 0, 1, and 2... for X, Y, and Z rotation respectively. So in the case of Z-only rotation, yes, it's column 2 :)I recommend using the EulerF deal combined with matrices... i.e.
MatrixF mat(EulerF(mDegToRad(45),0,0));
It seems like you know the rest. Oh, contruct the mat matrix as above, then after that do the setColumn(3, position) thing as before, then apply the setTransform for best results.
#3
I'm still a little confused though :-( John, does that line of code actually apply 45 degrees of rotation to the X axis? That's what it seems to do (well, to someone uninformed like me :-). I think what I need to do is try and calculate how much to rotate each axis after the ball has moved to give the impression of friction with the ground. The more I think about it, the more I think I may be in over my head... :-(
Is that a difficult thing to do? I expected it to be a case of specifying a value for X and Y rotation, if the ball wasn't 'spinning' on the Z axis. Am I oversimplyfying things, and missing the plot? Do I need a big slap? ;-)
Thanks again guys,
-Darren
06/30/2003 (4:44 am)
Thanks guys,I'm still a little confused though :-( John, does that line of code actually apply 45 degrees of rotation to the X axis? That's what it seems to do (well, to someone uninformed like me :-). I think what I need to do is try and calculate how much to rotate each axis after the ball has moved to give the impression of friction with the ground. The more I think about it, the more I think I may be in over my head... :-(
Is that a difficult thing to do? I expected it to be a case of specifying a value for X and Y rotation, if the ball wasn't 'spinning' on the Z axis. Am I oversimplyfying things, and missing the plot? Do I need a big slap? ;-)
Thanks again guys,
-Darren
#4
Ok, I'm at work, so this is all coming from memory. You'll likely have to play with the ordering. The idea is to first rotate around the z axis to the direction the ball is moving. This angle can be achieved using the "getAnglesFromVector" function. You'll just need the z componenent from that. Create your matrix as above with that Z rotation.
Next you'll need to rotate about the x axis. If you do it right, you shouldn't need to use the y axis. That is, as long as you use the actual travel direction (i.e. velocity), the x axis is all you need. Create another matrix like above with a different name. Then you'll call "firstMat.mul(secondMat);" This will combine the two rotations.
As far as figuring out how many degrees you need to rotate about x, you'll need the radius of your ball and the length of the velocity vector (if the item has velocity, then you just do F32 length = mVelocity.len(); ) Divide the velocity length by ( 2 * M_PI * radius ). Multiply this value by 360 degrees (or M_2PI for radians) and if I did all my math correctly, you should get a nifty rolling ball.
Good luck!
06/30/2003 (12:22 pm)
Ah, so you're trying to make a ball rolling on the ground. Well, that will take a fair bit of math. None too difficult, but math nonetheless.Ok, I'm at work, so this is all coming from memory. You'll likely have to play with the ordering. The idea is to first rotate around the z axis to the direction the ball is moving. This angle can be achieved using the "getAnglesFromVector" function. You'll just need the z componenent from that. Create your matrix as above with that Z rotation.
Next you'll need to rotate about the x axis. If you do it right, you shouldn't need to use the y axis. That is, as long as you use the actual travel direction (i.e. velocity), the x axis is all you need. Create another matrix like above with a different name. Then you'll call "firstMat.mul(secondMat);" This will combine the two rotations.
As far as figuring out how many degrees you need to rotate about x, you'll need the radius of your ball and the length of the velocity vector (if the item has velocity, then you just do F32 length = mVelocity.len(); ) Divide the velocity length by ( 2 * M_PI * radius ). Multiply this value by 360 degrees (or M_2PI for radians) and if I did all my math correctly, you should get a nifty rolling ball.
Good luck!
#5
I think I follow everything you've said. When 'creating' the first matrix, do I need to use the result from getTransform(), so I start off with the ball's previous position and rotation?
Also, I _may_ have a problem with the Velocity. Since I'm testing the ball on a flat surface (and probably won't make it any more complicated), I'm using a combination of:
Angle of travel (used to lookup into a 360*2 array of sin() and cos() calcs)
F32 Velocity
Do I need to ditch this system, and use a 3D Velocity? The reason I'm using a single value for velocity is that I couldn't figure out how to decay a 3D velocity - when you reduce a single velocity by friction, it's as simple as subtracting a value from it. However, if the ball's X speed is lower than it's Y speed, it will stop moving on the X axis before it stops moving on the Y axis :-(
I hope I'm not asking completely stupid questions here - if so, please feel free to say 'go read up on maths', I won't be offended :-)
I'll have a play about with this, and report back!
Thank you very much for your help John.
-Darren.
06/30/2003 (12:49 pm)
@John - Awesome, thanks for that! I see what you're doing with the Z and X rotation, I didn't think of it like that. I wish I had gone on to study this kind of thing :-(I think I follow everything you've said. When 'creating' the first matrix, do I need to use the result from getTransform(), so I start off with the ball's previous position and rotation?
Also, I _may_ have a problem with the Velocity. Since I'm testing the ball on a flat surface (and probably won't make it any more complicated), I'm using a combination of:
Angle of travel (used to lookup into a 360*2 array of sin() and cos() calcs)
F32 Velocity
Do I need to ditch this system, and use a 3D Velocity? The reason I'm using a single value for velocity is that I couldn't figure out how to decay a 3D velocity - when you reduce a single velocity by friction, it's as simple as subtracting a value from it. However, if the ball's X speed is lower than it's Y speed, it will stop moving on the X axis before it stops moving on the Y axis :-(
I hope I'm not asking completely stupid questions here - if so, please feel free to say 'go read up on maths', I won't be offended :-)
I'll have a play about with this, and report back!
Thank you very much for your help John.
-Darren.
#6
Seriously, I'm happy to help, and will give as much advice as time permits, but it won't hurt to learn some of it yourself... if you're in college now, take a Linear Algebra class... if you're before that or after that... there are some articles on gamedev.net and a couple decent books. I can make recommendations if you choose that route.
Anyway... let's look at your questions... [strokes scruff on chin]
Ok, using a 3D vector is definitely advisable. To dampen/decay a 3D vector in Torque, it looks like this:
As far as using getTransform for previous values, it depends on how you want to do it. Definitely use it to get the position. Something like:
With rotation, I think the route I would take is to make it fresh every time. getAnglesFromVector will return the total z rotation, not just the change since last update. And for the x rotation, as long as you multiply the rotation or distance or something by TickSec, you should get good results starting fresh every time as well. I mean by that: xAngle *= TickSec; after you have calculated it.
Happy coding!
06/30/2003 (11:42 pm)
Go read up on maths :)Seriously, I'm happy to help, and will give as much advice as time permits, but it won't hurt to learn some of it yourself... if you're in college now, take a Linear Algebra class... if you're before that or after that... there are some articles on gamedev.net and a couple decent books. I can make recommendations if you choose that route.
Anyway... let's look at your questions... [strokes scruff on chin]
Ok, using a 3D vector is definitely advisable. To dampen/decay a 3D vector in Torque, it looks like this:
mVelocity *= 0.95;Use whatever factor you need... greater than one will lengthen it, less than one (but positive) will shrink it. It will still point the same direction. When you use getAnglesFromVector, the z rotation will be accurate, because to get that it essentially "looks from the top down" to get that angle. The other angles aren't important for this (unless you plan on rolling up walls and switching gravity around). Be sure to check for a zero-length vector when doing most of these calculations ( mVelocity.len() == 0) or ( mVelocity.isZero() ). If the velocity is zero, you could get some divide-by-zero errors if you're not careful.
As far as using getTransform for previous values, it depends on how you want to do it. Definitely use it to get the position. Something like:
Point3F pos; previousMatrix.getColumn(3, &pos);With tweaking the syntax, of course.
With rotation, I think the route I would take is to make it fresh every time. getAnglesFromVector will return the total z rotation, not just the change since last update. And for the x rotation, as long as you multiply the rotation or distance or something by TickSec, you should get good results starting fresh every time as well. I mean by that: xAngle *= TickSec; after you have calculated it.
Happy coding!
#7
Somehow I knew you were going to say that :-D
Well, again, thank you very much for this advice. Again, you've explained exactly what I needed to know, and in terms that I can understand. I get what you're saying with the 3D Velocity - I actually worked out myself (last night, around 1:00 AM :-) that if I was to use the 3D Velocity I would need to either divide or multiply the three values, and you've solved the syntax for me above!
I think, for the actual rotation, I first need to get a better idea of how and when the server-side objects are updated. To be honest, I haven't removed too much code from the Item class since I made the copy and renamed, and transformations are applied all over the place - processTick(), interpolateTick(), unpackUpdate() etc. I'm getting my head around this stuff now, and will hopefully be able to 'clean down' the code, removing anything that is unneccesary, and replacing it with my own code. With the first few attempts at rotation, the Ball ignorantly continued to rotate on the Z axis, even when I thought I'd removed the original rotation code! :-) Not exactly making the problem at hand easier... All good fun eh?
Well, I'll keep hammering away at this, and a few other ideas I'm trying to work on. It's certainly nice to know that I can get this quality of help and advice from the community, and I'd like to thank you again for your time!
Oh, I'm 27 by the way - a little past college age I'm afraid, but still very willing to learn! Would you mind making those book recommendations you mentioned? I'd certainly like to get into this kind of thing, and it is rather important in the grand scheme of things!
Many thanks John,
Darren.
07/01/2003 (7:11 am)
Hi John,Somehow I knew you were going to say that :-D
Well, again, thank you very much for this advice. Again, you've explained exactly what I needed to know, and in terms that I can understand. I get what you're saying with the 3D Velocity - I actually worked out myself (last night, around 1:00 AM :-) that if I was to use the 3D Velocity I would need to either divide or multiply the three values, and you've solved the syntax for me above!
I think, for the actual rotation, I first need to get a better idea of how and when the server-side objects are updated. To be honest, I haven't removed too much code from the Item class since I made the copy and renamed, and transformations are applied all over the place - processTick(), interpolateTick(), unpackUpdate() etc. I'm getting my head around this stuff now, and will hopefully be able to 'clean down' the code, removing anything that is unneccesary, and replacing it with my own code. With the first few attempts at rotation, the Ball ignorantly continued to rotate on the Z axis, even when I thought I'd removed the original rotation code! :-) Not exactly making the problem at hand easier... All good fun eh?
Well, I'll keep hammering away at this, and a few other ideas I'm trying to work on. It's certainly nice to know that I can get this quality of help and advice from the community, and I'd like to thank you again for your time!
Oh, I'm 27 by the way - a little past college age I'm afraid, but still very willing to learn! Would you mind making those book recommendations you mentioned? I'd certainly like to get into this kind of thing, and it is rather important in the grand scheme of things!
Many thanks John,
Darren.
#8
Here's the first thing I would recommend... a tutorial on the basics.
gamedev.net/reference/articles/article1832.asp
This is one of the better primers I have seen on vectors and matrices. It's rather lengthy... don't read it in one sitting. I would maybe cover vectors one day and take two days on matrices. Practicing on paper will help you remember the stuff.
As far as more advanced stuff goes, it's hard to say. Mathematics for 3D Game Programming and Computer Graphics is a pretty good book, though it goes into some of the rather unnecessary theory (which comes as a result of it hoping to be a school textbook). The good thing is it goes into some of the more complicated stuff you'll be hard pressed to find online somewhere. You can get it from half.com for around $35 usually.
Here's a list of things I would recommend studying (for which you should be able to find tutorials online):
Vector arithmetic, matrix arithmetic (esp. multiplication), dot product, cross product, matrix inversion, projecting one vector onto another, finding angles between two vectors (w/ dot product), orthonormal basis, change-of-basis matrix (excellent for rotating an object to line-up with an up, side, and forward vector), basic trigonometry (calculating angles in triangles, esp. right-triangles)... that's about all I can think of at the moment. If you can't find a tutorial on one of these topics, then I should probably take it upon myself to write one... since I use these things quite commonly as tools to achieve the effect I want to see.
Anyone else reading this thread feel free to chip in on important topics I forgot... as well as recommended resources. I do know there are such resources even on this website...
07/01/2003 (1:02 pm)
Ok,Here's the first thing I would recommend... a tutorial on the basics.
gamedev.net/reference/articles/article1832.asp
This is one of the better primers I have seen on vectors and matrices. It's rather lengthy... don't read it in one sitting. I would maybe cover vectors one day and take two days on matrices. Practicing on paper will help you remember the stuff.
As far as more advanced stuff goes, it's hard to say. Mathematics for 3D Game Programming and Computer Graphics is a pretty good book, though it goes into some of the rather unnecessary theory (which comes as a result of it hoping to be a school textbook). The good thing is it goes into some of the more complicated stuff you'll be hard pressed to find online somewhere. You can get it from half.com for around $35 usually.
Here's a list of things I would recommend studying (for which you should be able to find tutorials online):
Vector arithmetic, matrix arithmetic (esp. multiplication), dot product, cross product, matrix inversion, projecting one vector onto another, finding angles between two vectors (w/ dot product), orthonormal basis, change-of-basis matrix (excellent for rotating an object to line-up with an up, side, and forward vector), basic trigonometry (calculating angles in triangles, esp. right-triangles)... that's about all I can think of at the moment. If you can't find a tutorial on one of these topics, then I should probably take it upon myself to write one... since I use these things quite commonly as tools to achieve the effect I want to see.
Anyone else reading this thread feel free to chip in on important topics I forgot... as well as recommended resources. I do know there are such resources even on this website...
#9
Once again, thanks a lot - that's great advice. I started looking at the gamedev article (late nights again :-), and will be sure to devote a few days to reading it properly soon. I followed everything I read there, which is a promising sign for the rest of the tutorial. I think my main problem with linear algebra (and I suppose many other people's too) is that I don't fully understand the notation - I'm looking forward to learning that because I pretty much understand matrix stuff when it's explained with [] notation. It's when it gets to 'so, we can simplify that and write it as 2P=./p||' that it starts to get difficult :-)
I've noted all that down, and will be sure to read up on it soon. At the moment, I'm writing a VB business app to sell to someone (yawn...). As soon as that's done (1-2 weeks), I'm free to work on the games stuff full time. Yay!
I know I've already thanked you for this advice several times, but you understood my problem very quickly, and the quality of the advice you've given is excellent. Very clear explanations, for what is potentially a very difficult subject! I think if you were to write any resources at all on these kinds of problems, you would be doing many people a huge favour. I am very confident in my general design and development abilities (I've been programming on and off since I was 10 :-), but the math stuff always created a large hole in my confidence of creating 3D games. I belive there are many people in my position, who have a solid development background but don't have advanced maths and physics knowledge. I also believe there will be a lot of these people sat at home with Torque, getting to a certain point with their ideas, and giving up due to the 'assumed' complexity. Maybe a resource on this kind of stuff would fill that confidence hole for more people, which could only be a good thing. Sorry, I wander off the subject sometimes...
On a slightly different note: I don't know if you are actually on a Team at the moment, or working on a Torque game yourself. However, if you have the time it could be worth checking out the 'build/find a team' area of the site - I'm sure with your understanding of these topics you would make a very valuable team member ($$$ :-) In fact, if you have the time, would you be interested in hearing one of my ideas? I have a clear, solid design for a game, I'm 99.9% sure it would be hugely popular, and it appeals to a very wide audience. I have thought through the important aspects of the game (utilising the biggest benefits Torque has to offer), and I also have a (friend) 3D Modeller producing some initial art for the game. I will be able to work on the game code full-time over the next 6 months (or more), which is all think it will take to complete the first idea. It looks very promising, but the physics code will cause problems until I can learn the math. I'm very serious about doing this idea (and 2 or 3 others I have, after that) - let me know if you're at all interested, and I'll give you a proper idea of the game itself. I'm not pretending to be James Bond or anything, but I'd prefer not to give the idea to someone who didn't already have it, and can develop it faster :-)
Thanks again John,
Darren.
07/02/2003 (4:53 am)
John,Once again, thanks a lot - that's great advice. I started looking at the gamedev article (late nights again :-), and will be sure to devote a few days to reading it properly soon. I followed everything I read there, which is a promising sign for the rest of the tutorial. I think my main problem with linear algebra (and I suppose many other people's too) is that I don't fully understand the notation - I'm looking forward to learning that because I pretty much understand matrix stuff when it's explained with [] notation. It's when it gets to 'so, we can simplify that and write it as 2P=./p||' that it starts to get difficult :-)
I've noted all that down, and will be sure to read up on it soon. At the moment, I'm writing a VB business app to sell to someone (yawn...). As soon as that's done (1-2 weeks), I'm free to work on the games stuff full time. Yay!
I know I've already thanked you for this advice several times, but you understood my problem very quickly, and the quality of the advice you've given is excellent. Very clear explanations, for what is potentially a very difficult subject! I think if you were to write any resources at all on these kinds of problems, you would be doing many people a huge favour. I am very confident in my general design and development abilities (I've been programming on and off since I was 10 :-), but the math stuff always created a large hole in my confidence of creating 3D games. I belive there are many people in my position, who have a solid development background but don't have advanced maths and physics knowledge. I also believe there will be a lot of these people sat at home with Torque, getting to a certain point with their ideas, and giving up due to the 'assumed' complexity. Maybe a resource on this kind of stuff would fill that confidence hole for more people, which could only be a good thing. Sorry, I wander off the subject sometimes...
On a slightly different note: I don't know if you are actually on a Team at the moment, or working on a Torque game yourself. However, if you have the time it could be worth checking out the 'build/find a team' area of the site - I'm sure with your understanding of these topics you would make a very valuable team member ($$$ :-) In fact, if you have the time, would you be interested in hearing one of my ideas? I have a clear, solid design for a game, I'm 99.9% sure it would be hugely popular, and it appeals to a very wide audience. I have thought through the important aspects of the game (utilising the biggest benefits Torque has to offer), and I also have a (friend) 3D Modeller producing some initial art for the game. I will be able to work on the game code full-time over the next 6 months (or more), which is all think it will take to complete the first idea. It looks very promising, but the physics code will cause problems until I can learn the math. I'm very serious about doing this idea (and 2 or 3 others I have, after that) - let me know if you're at all interested, and I'll give you a proper idea of the game itself. I'm not pretending to be James Bond or anything, but I'd prefer not to give the idea to someone who didn't already have it, and can develop it faster :-)
Thanks again John,
Darren.
#10
Go ahead and drop me a line at: tron AT unicyclist.com and we can discuss some off-topic stuff.
07/02/2003 (8:41 am)
Darren,Go ahead and drop me a line at: tron AT unicyclist.com and we can discuss some off-topic stuff.
#11
I'm trying to drop you an email at the moment, but my ISP seems to have misplaced their mail server. Hopefully they'll find it again soon :-)
Darren.
07/02/2003 (3:52 pm)
Thanks John,I'm trying to drop you an email at the moment, but my ISP seems to have misplaced their mail server. Hopefully they'll find it again soon :-)
Darren.
Torque 3D Owner Xavier "eXoDuS" Amado
Default Studio Name