I am programming Starcraft 2 custom maps and got some proglems with math in 3D. Currently I am trying to create and rotate a point around an arbitrary axis, given by x,y and z (the xyz vector is normalized).
I've been trying around a lot and read through a lot of stuff on the internet, but I just cant get how it works correctly. My current script (you probably dont know the language, but it's nothing special) is the result of breaking everything for hours (doesn't work correctly):
point CP; fixed AXY; point D; point DnoZ; point DXY_Z; fixed AZ; fixed LXY; missile[Missile].Angle = (missile[Missile].Angle + missile[Missile].Acceleration) % 360.0; missile[Missile].Acceleration += missile[Missile].AirResistance; if (missile[Missile].Parent > -1) { D = missile[missile[Missile].Parent].Direction; DnoZ = Point(PointGetX(D),0.0); DXY_Z = Normalize(Point(SquareRoot(PointDot(DnoZ,DnoZ)),PointGetHeight(D))); AZ = MaxF(ACos(PointGetX(DXY_Z)),ASin(PointGetY(DXY_Z)))+missile[Missile].Angle; DnoZ = Normalize(DnoZ); AXY = MaxF(ACos(PointGetX(DnoZ)),ASin(PointGetY(DnoZ))); CP = Point(Cos(AXY+90),Sin(AXY+90)); LXY = SquareRoot(PointDot(CP,CP)); if (LXY > 0) { CP = PointMult(CP,Cos(AZ)/LXY); PointSetHeight(CP,Sin(AZ)); } else { CP = Point3(0.0,0.0,1.0); } } else { CP = Point(Cos(missile[Missile].Angle),Sin(missile[Missile].Angle)); } missile[Missile].Direction = Normalize(CP); missile[Missile].Position = PointAdd(missile[Missile].Position,PointMult(missile[Missile].Direction,missile[Missile].Distance));
I just cant get my mind around the math. If you can explain it in simple terms that would be the best solution, a code snipped would be good as well (but not quite as helpful, because I plan to do more 3D stuff in the future).
Rotate space about the x axis so that the rotation axis lies in the xz plane. Let U = (a,b,c) be the unit vector along the rotation axis. and define d = sqrt(b2 + c2) as the length of the projection onto the yz plane. If d = 0 then the rotation axis is along the x axis and no additional rotation is necessary.
Rotation about an arbitrary point: If we want to rotate an object or point about an arbitrary point, first of all, we translate the point about which we want to rotate to the origin. Then rotate point or object about the origin, and at the end, we again translate it to the original place.
The Arbitrary Rotation command rotates a layer by a specified angle. It is an alternate way of accessing the Rotate tool.
“Arbitrary” means you may choose the axis of rotation any way you like. So simply choose any line in space, and then put a pin into your object along that line, and rotate the object around the pin.
http://en.wikipedia.org/wiki/Rotation_matrix. Look under the section Rotation matrix from axis and angle. For your convenience, here's the matrix you need. It's a bit hairy. theta is the angle, and ux, uy, and uz are the x, y, and z components of the normalized axis vector
If you don't understand matrices and vectors, post back and I'll help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With