xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 03:11 PM |
I'm trying to make a 3D game in C++.
I was having trouble with rotations around points, but then I remembered the nice CFrame class I used back then in RBX.lua. I was wondering how I would go about creating something similar. Sadly, I didn't get very far because I don't know how to use rotation matrices, and computing euler angles to a direction vector using a one-liner quickly got a bit too complicated. Are there any open-source goodies that offer similar functionality? I would also prefer to have it stand-alone, so without any bloated physics libraries coming with it.
Thank you in advance. ~ss1122 |
|
|
| Report Abuse |
|
|
mustyoshi
|
  |
 |
| Joined: 27 Dec 2007 |
| Total Posts: 41651 |
|
|
| 24 Jul 2012 03:19 PM |
I started writing a 2D and then 3D Vector class. It was all templated so I could use any datasize.
~Monica |
|
|
| Report Abuse |
|
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 03:22 PM |
| Yeah, I did that too. Worked wonderfully. However, like I was saying, things got messy when I tried to implement rotation-related things. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 24 Jul 2012 03:22 PM |
Look at Cinder.
It's got all of that math stuff build in, and integrated with the OpenGL / DirectX to boot. |
|
|
| Report Abuse |
|
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 03:39 PM |
| Oh dear, I don't see any Makefile or configuration script... |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 24 Jul 2012 03:59 PM |
| If you don't know how to work a matrix, you shouldn't be making a 3D game. |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2012 05:29 PM |
CFrames are just matricies, most 3d libraries have them
|
|
|
| Report Abuse |
|
|
| |
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 09:50 PM |
| I couldn't get Cinder to work on Ubuntu, so I decided to myself try again. At least I got the lookVector working. Instead of storing the rotation with a matrix of some sort, I used euler angles. This made me wonder, though, what is so great about rotation matrices that make them worth taking up three times as much space? |
|
|
| Report Abuse |
|
|
|
| 24 Jul 2012 10:05 PM |
"This made me wonder, though, what is so great about rotation matrices that make them worth taking up three times as much space?"
They're computationally less expensive and the memory impact is minimal in the long wrong because it ends up evening out. One matrix can store rotation, scale, and position of a point (though you won't easily be able to extract each of those elements from it, you shouldn't need to and if you need them you should be storing them elsewhere). Matrix operations are also fairly simple and can be quicker than any methods to rotate, scale, and shift a set of points based on vectors of just rotation, position, and scale. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 24 Jul 2012 10:43 PM |
To elaborate:
The main thing is that trigonometric functions are _far_ more expensive than you think. You can do a good 100 matirx multiplications in the time it would take you to calculate a _single_ operation on a set of angles.
On top of that, a matrix can not only encode a rotation, but a translation, a scale, or even some other less common operations such as skewing. And it does all this with no special code needed at all, the code to multiply the matrices will be the same regardless of what sort of transformations you're storing in them.
Also due to the way computers work it's not only somewhat easier, but a lot easier to do operations on matrices. You can do an entire matrix multiply in only a dozen or so CPU instructions on modern x86 processors. |
|
|
| Report Abuse |
|
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 11:15 PM |
I'm amazed your complicated matrix magic is actually computionally less expensive than using angles, but I was probably doing something wrong while trying to learn how to use them. I suppose I'll notice my way of doing things sucks later on though, but for now:
So far the following seems to work for the lookVector. sinX, sinY, cosX and cosY are static floats. I noticed I didn't need to use the Z angle, but I suppose that makes sense.
sincosf(rotation.x, &sinX, &cosX); sincosf(rotation.y, &sinY, &cosY); return Vector3(cosX * sinY, sinX, cosX * cosY);
I would appreciate any help on converting a lookVector back to angles and vectorToWorldSpace. These things are specifically what I failed at last time. Would something such as the following perhaps work for vectorToWorldSpace?
Vector3 CFrame::VectorToWorldSpace(const Vector3& vector) { /* In RBX.lua, this expression would be: return (cframe * CFrame.new(Vector3.new(), vector)).lookVector * vector.magnitude */ return operator *(CFrame(Vector3(), vector)).getLookVector() * vector.getMagnitude(); }
Ideas for the constructor CFrame(Vector3 position, Vector3 point) are welcome.
Once again, thank you in advance. ~ss1122 |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 24 Jul 2012 11:36 PM |
Hey xa13nx,
I'm not sure if you can find a way to relate the following to your issue, but I did figure out a way to convert angles into a 3x3 rotation matrix and back again. Here:
ch = math.cos(attitude) -- Y sh = math.sin(attitude) ca = math.cos(bank) -- Z sa = math.sin(bank) cb = math.cos(heading) -- X sb = math.sin(heading)
matrix = CFrame.new( x,y,z, -- The positional values ch * ca, sh*sb - ch*sa*cb, ch*sa*sb + sh*cb, -- m00, m01, m02 sa, ca*cb, -ca*sb, -- m10, m11, m12 -sh*ca, sh*sa*cb + ch*sb, -sh*sa*sb + ch*cb -- m20, m21, m22 )
local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = matrix:components() heading = math.atan2(-m12,m22) -- angle x attitude = math.asin(m02) -- angle y bank = math.atan2(m01, m00) -- angle z
|
|
|
| Report Abuse |
|
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 24 Jul 2012 11:54 PM |
| I'm afraid that converting between the two all the time pretty much negates the benefits of using a matrix; aside from potential ways to implement fromEulerAngles and toEulerAngles, for when I finally stop being an idiot and switch to using a matrix. I guess I should just try again and learn how to properly use one :c |
|
|
| Report Abuse |
|
|
xa13nx
|
  |
| Joined: 09 Jan 2010 |
| Total Posts: 28 |
|
|
| 25 Jul 2012 12:03 AM |
On a side node, I've been banned for about 9 months, and people are still posting nostalgic comments on my place once every few days. Isn't that just so nice? Please forgive me, that was quite off topic.
Anyway, I tried learning about rotation matrices from wikipedia, but I don't understand most of the mathematic terms used. Are there any guides for oblivious teenagers?
Yet again, thank you in advance. ~ss1122 |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
| |
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
| |
|
Combrad
|
  |
| Joined: 18 Jul 2009 |
| Total Posts: 11025 |
|
| |
|
dekkonot
|
  |
| Joined: 22 Dec 2010 |
| Total Posts: 6685 |
|
|
| 27 Sep 2013 05:06 PM |
(eye twitch)
~ Linguam latinam est optimum ~ |
|
|
| Report Abuse |
|
|
Victimize
|
  |
| Joined: 09 Jun 2013 |
| Total Posts: 1789 |
|
|
| 27 Sep 2013 05:10 PM |
do people seriously go back to like page 300 just to apparently bump this like seriously |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 27 Sep 2013 05:17 PM |
| Do not underestimate the limit of stupid. |
|
|
| Report Abuse |
|
|