3D Rotation Algorithm?

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Post Reply
Baldassari
Newbie
Posts: 3
Joined: Sat Jan 26, 2008 11:35 pm
Location: Texas

3D Rotation Algorithm?

Post by Baldassari »

If I'm crazy go ahead and tell me, but it seems that the following should be possible, if not actually rather simple:

I'm looking for an algorithm to simplify 3D (it doesn't have to be EXTREME '28th Century' 3D) graphics rotation, specifically regarding image rotation along the Z-axis.

Think of the old Atari coin-op game, "Battlezone". The enemy tanks (which were vector "draws") could rotate 180 degrees, by increments of around 10. They could face the player, face perpendicularly to the player in either direction, or be aligned along any of several angles in between. I would like to be able to make images do essentially the same thing, without having to compose (and code) every image. Here's what I'm thinking:

There ought to be a way to write a program to plot an image at any angle between 0 and 90. One should be able to provide this program with two images, a "front" (0 degree), and a "side" (90 degree) image. The program would, having read both images, utilize the algorithm to provide a corresponding (by most measures it would actually be a "conglomerate") image for the given angle (between 1 and 89).

Of course, one might well write a program to calculate said algorithm, but such would be way beyond my means.

I searched the web for hours for an answer before coming here to clutter up this forum. Any help would be greatly appreciated.
Baldassari
Newbie
Posts: 3
Joined: Sat Jan 26, 2008 11:35 pm
Location: Texas

Post by Baldassari »

I would especially appreciate any help before carrying a digital camera out to my workshop, cutting a length of 2x4, painting to outline its edges, decorating two of its sides, photographing it at 0, 10, 20, 30, 40, 50, 60, 70, 80, and 90 degrees relative the camera, uploading the images, and working for hours to approximate the algorithm in question!!!

Heh.
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

Ehm, you mean 2d rotation I believe? Like a rotozoomer? The formula for that is:
RotatedX = ScreenX * COS(angle) - ScreenY * SIN(angle)
RotatedY = ScreenX * SIN(angle) + ScreenY * COS(angle)

For more info, check:
The first two are about rotation only. The thirs is about rotozoomers (so also zooming and optimization, this may be good if you want to use it in a game. Check the example code too)
http://www.petesqbsite.com/sections/tut ... ation.html
http://www.petesqbsite.com/sections/tut ... tation.txt
http://www.petesqbsite.com/sections/tut ... otate.html
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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Nothing is SIMPLE when you don't know! Especially if it involves a 2 by 4!

How many sites are you gonna post this on?

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Baldassari
Newbie
Posts: 3
Joined: Sat Jan 26, 2008 11:35 pm
Location: Texas

Post by Baldassari »

Hey! 2x4's, second only to Petroleum, make all you know of The World go 'round!

Heh!

I only posted on the 2 websites of which you are (apparently) aware. I have a bad habit of drinking and driving (gin and surfing), I must admit, and I wanted an answer to this question!

I think that I made my tragic mistake by misnaming the axis in question. fork the Z-axis, boys. I want to rotate an image about the Y axis.

Y.

As in WHY?

We're talking Y.

ahum.

ehm.

Anyway~
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Oh dear. Not these conventions again. Do you know how to multiply matrices? Not code them in, but matrices do clarify it up quite a bit.

On a 2d coordinate system:
let R be the roational matrix
let P be the set of 2d points
let Q be the point you want

Code: Select all

Q=R*P
where:

R= [[cos(Θ)  -sin(Θ)]
    [sin(Θ)   cos(Θ)]]

P= [[x]
    [y]]

Q= [[new x]
    [new y]]

Θ is the rotation angle, clockwise or ccw is based on the sign. Play around to see which direction. 
In your case, just switch the 'y's with the 'z's . This is the same thing as Codemss' presented equations, though in a shorter notation. DirectX and OpenGL use rotational matrices. If you can create a matrice class, and some functions for them (methods possibly), the work thereafter is very simple. Then a matrice can take care of 3d rotation, scaling, reflection, and other little manipulations. All you have to so is to know what numbers go where.

My rant probably seems hard to understand. I didn't understand it at first, and it took me some time to figure it out. But if you're going into 3d graphics and design, matrices are a very important tool.

I've got some programs that can do 3d graphics and rotation (with a movable camera!).

You also might want to use a depth array (is it a buffer?) so that things in the back can't be drawn above things in the front. But it'll take some work.
For any grievances posted above, I blame whoever is in charge . . .
Post Reply