I've been reading through several of the 3D tutorials. I understand the basics, but I'm having issues with incorperating rotation formulas into my programs. Keeping it simple, How do I rotate around a point rather than the axies. what type of transformation do I need to apply to the basic formula?
(transformations for the 2d formulas would be nice as well...)
3D Rotation Question
you always rotate around an axis and the origin. once you've completed your rotation you translate your points to an object location to make it appear as if they rotated around some remote location. heres an example using formulas only, no matricies. this was the way i learned
Code: Select all
'--- simple 3d engine by syn9
screen 18,,2
dim as integer apage, bpage
dim as single rootX, rootY, rootZ, rotatedX, rotatedY, rotatedZ, x, z, angle
dim as string k
apage = 0: bpage = 1
rootY = -6
do
'--- setup display
screenset apage,bpage
swap apage,bpage
cls
'--- input handling
k = inkey
if k = chr(27) then exit do
if k = "s" then rootX-=.1
if k = "f" then rootX+=.1
if k = "e" then rootY-=.1
if k = "d" then rootY+=.1
if k = "w" then rootZ-=.1
if k = "r" then rootz+=.1
print rootX, rootY, rootZ
'--- rotate point x,0,z around 0,0,0
angle += .1
for x = -10 to 10
for z = -10 to 10
'--- first rotate points around the origin (0,0,0)
rotatedX = x * cos(3.14159 / 180 * angle) - z * sin(3.14159 / 180 * angle)
rotatedZ = x * sin(3.14159 / 180 * angle) + z * cos(3.14159 / 180 * angle)
'--- translate rotated point to the object location (rootX,0,rootZ)
rotatedX -= rootX
rotatedY = rootY 'we didnt rotate on the y or z axis, so this didnt change
rotatedZ -= rootZ
'--- translate camera away from the points so we can see them
rotatedZ -= 25
'--- project 3d coordinates to screen coordinates
if rotatedZ < 0 then 'if > 0 then point is behind the camera
screenX = 320 + rotatedX / rotatedZ * 256
screenY = 240 + rotatedY / rotatedZ * 256
pset (screenX, screenY), 10
end if
next
next
loop
- Kiyotewolf
- Veteran
- Posts: 96
- Joined: Tue Apr 01, 2008 11:38 pm
rotating 3d around a point
When you want to rotate around a point, you have to translate, (add or subtract in x, y & z,) so that your point becomes 0,0,0, and the other points still have their relative positions but have been shifted accordingly.
Now, when you apply the 3D rotation math to your points, every point that needs to be changed, moved around that point that you want, will rotate as if the 0,0,0 was the center of the universe, and all you have to do is un-translate, (reverse the math to move your 0,0,0 point back to it's original value, say.. -3, 11, -25), and all the points will now be back to their original places, but the points surrounding your centeroid are going to be rotated around that singular point you chose to offset everything by.
So, do matrix addition and subraction, ..
x(1)=-3
y(1)=11
z(1)=-25
for z = 2 to MaxPoint
x(z)=x(z)+(-1 * (x(1))
y(z)=y(z)+(-1*(y(1))
z(z)=z(z)+(-1*(z(1))
next z
Now, do your 3D rotation math.
~ Don't remember how that goes.. but you have that already.. ~
Now, reverse those calculations using another FOR .. NEXT loop to translate your points back to the space they came from, and voila.
Of course, this example would crash in FB, because the variable name and the array name are the same,.. z & z( ), so you will have to change this to get it to work in FB.
Kiyote!
Now, when you apply the 3D rotation math to your points, every point that needs to be changed, moved around that point that you want, will rotate as if the 0,0,0 was the center of the universe, and all you have to do is un-translate, (reverse the math to move your 0,0,0 point back to it's original value, say.. -3, 11, -25), and all the points will now be back to their original places, but the points surrounding your centeroid are going to be rotated around that singular point you chose to offset everything by.
So, do matrix addition and subraction, ..
x(1)=-3
y(1)=11
z(1)=-25
for z = 2 to MaxPoint
x(z)=x(z)+(-1 * (x(1))
y(z)=y(z)+(-1*(y(1))
z(z)=z(z)+(-1*(z(1))
next z
Now, do your 3D rotation math.
~ Don't remember how that goes.. but you have that already.. ~
Now, reverse those calculations using another FOR .. NEXT loop to translate your points back to the space they came from, and voila.
Of course, this example would crash in FB, because the variable name and the array name are the same,.. z & z( ), so you will have to change this to get it to work in FB.
Kiyote!
Banana phone! We need more lemon pledge. * exploding fist of iced tea! * I see your psycho cat and counter with a duck that has a broken leg, in a cast.
- Codemss
- Veteran
- Posts: 124
- Joined: Sun Jun 24, 2007 6:49 am
- Location: Utrecht, The Netherlands
- Contact:
Usually, you translate, rotate and translate again. You can avoid that first translate if you have a static(not-moving) rotation point. Then you translate everything one time and save the new points. Then you just rotate and translate...
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
- Kiyotewolf
- Veteran
- Posts: 96
- Joined: Tue Apr 01, 2008 11:38 pm
oh.. right
Right,.. you are rotating every other point,..
so.. you wouldn't need to translate your stationary point, just every other point around it..
I finally found a stable version of Freebasic & an IDE editor combo that works together.. I'm going to start tinkering in 3D again I think.. ^o^
._.' Freebasic seems to me the same line of thinking when I went from DOS Turbo Pascal and converted over to "" something "" Windows TP..
Playing with API's and DLL's is fun.. s'pecially when you tweak Windows into doing something you shouldn't be able to do.
so.. you wouldn't need to translate your stationary point, just every other point around it..
I finally found a stable version of Freebasic & an IDE editor combo that works together.. I'm going to start tinkering in 3D again I think.. ^o^
._.' Freebasic seems to me the same line of thinking when I went from DOS Turbo Pascal and converted over to "" something "" Windows TP..
Playing with API's and DLL's is fun.. s'pecially when you tweak Windows into doing something you shouldn't be able to do.
Banana phone! We need more lemon pledge. * exploding fist of iced tea! * I see your psycho cat and counter with a duck that has a broken leg, in a cast.