It looks like you're new here. If you want to get involved, click one of these buttons!

- 140.8K All Categories
- 103.7K Programming Languages
- 6.5K Assembler Developer
- 1.9K Basic
- 40K C and C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.7K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 546 Python
- 37 Ruby
- 4.4K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 315 Console programming
- 91 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 741 Computer Hardware
- 3.4K Database & SQL
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 257 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 198 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 603 Jobs Wanted
- 209 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 3.4K Miscellaneous
- 8 Join the Team
- 354 Comments on this site
- 69 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 627 Off topic board
- 201 Mobile & Wireless
- 73 Android
- 126 Palm Pilot
- 338 Multimedia
- 154 Demo programming
- 184 MP3 programming
- Bash scripts
- 27 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 370 MS-DOS
- Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 940 Software Development
- 417 Algorithms
- 68 Object Orientation
- 91 Project Management
- 95 Quality & Testing
- 268 Security
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 4 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 37 JQuery
- 305 WEB Servers
- 150 WEB-Services / SOAP

Lopez
Member Posts: **75**

I've coded a 3D plotter, which takes the 3 x,y,z co-ordinates and puts them to the screen. I've made a cube but I can only move the camera x,y,z I want to make the camera be able to rotate it's viewpoint along x axis (or y axis, both are the same), what is the technique for doing this?

or the technique for rotating the cube?

Someone must know the technique, I've seen so many rotating cube demos in my life. Does it involve the equation of a circle?

or the technique for rotating the cube?

Someone must know the technique, I've seen so many rotating cube demos in my life. Does it involve the equation of a circle?

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven

© 1997-2015 Programmersheaven.com - All rights reserved.

## Comments

75Um, I think this is right anyway, see if I can get it right now.

22A little trig:

x=r*cos(a)

y=r*sin(a)

[x^2+y^2=(r*cos(a))^2+(r*sin(a))^2 ;normal/polar circle eq]

Now we do a rotation of angle b:

x=r*cos(a+b); [cos(a+b)=cos(a)*cos(b)-sin(a)*sin(b)]

y=r*sin(a+b); [sin(a+b)=sin(a)*cos(b)+cos(a)*sin(b)]

x=r*cos(a)+cos(b)-r*sin(a)*sin(b); [r*cos(a)=x, r*sin(a)=y]

y=r*sin(a)+cos(b)+r*cos(a)*sin(b); [r*cos(a)=x, r*sin(a)=y]

Finally:

x=x*cos(b)-y*sin(b)

y=x*sin(b)+y*cos(b)

Now if we look from -Z to +Z we see:

X

|_Y 2Dplane(x,y)

From -X to +X:

Z

|_Y 2Dplane(y,z)

From -Y to +Y:

X

|_Z 2Dplane(z,x)

Then the rotation around X-Axis of angle ax:

x=x

y=y*cos(ax)-z*sin(ax)

z=y*sin(ax)+z*cos(ax)

Y-Axis ay:

x=z*sin(ay)+x*cos(ay)

y=y

z=z*cos(ay)-x*sin(ay)

Z-Axis az:

x=x*cos(az)-y*sin(az)

y=x*sin(az)+y*cos(az)

z=z

2-Axis rotation takes: 8 mul, 2 add, 2 sub per point

Rotate of 25 on ax then 30 on ay, or first 30 on ay then 25 on ax dont change the result, so the order we rotate ours point dont actually matter.

Matrix: Rotate using a matrix

Z-Axis: (that u dont need but it's simple to understand)

x=x*( cos(az)) + y*(-sin(az)) + z*0

y=x*( sin(az)) + y*( cos(az)) + z*0

z=x*0 + y*0 + z*1

This sys of eq. can be express as the multiplication betwenn a 3x3 matrix and a 3D vector:

V'=Rz*V; [Rz:matrix, V:vector, V':rotated vector]

The coefficent of Rz are:

Rz0=cos(az), Rz1=-sin(az), Rz2=0,

Rz3=sin(az), Rz4=cos(az), Rz5=0,

Rz6=0, Rz7=0, Rz8=1

where:

x = x*Rz0 + y*Rz1 + z*Rz2

y = x*Rz3 + y*Rz4 + z*Rz5

z = x*Rz6 + y*Rz7 + z*Rz8

The coefficent of Ry:

Ry0=cos(ay), Rz1=0, Rz2=sin(ay),

Ry3=0, Rz4=1, Rz5=0,

Ry6=-sin(ay), Rz7=0, Rz8=cos(ay)

The coefficent of Rx:

Rx0=1, Rz1=0, Rz2=0,

Rx3=0, Rz4=cos(ax), Rz5=-sin(ax),

Rx6=0, Rz7=sin(ax), Rz8=cos(ay)

The multiplication of two matrixs create a thirth matrix that does the both 3d trasformation in the order of the multiplication, with rotation dosn't really matter but with matrixs u can perfor other 3D traformation where the order is important indeed.

Rx*Ry=Rxy:

Rxy0 = Rx0*Ry0 + Rx1*Ry3 + Rx2*Ry6

Rxy1 = Rx0*Ry1 + Rx1*Ry4 + Rx2*Ry7

Rxy2 = Rx0*Ry2 + Rx1*Ry5 + Rx2*Ry8

Rxy3 = Rx3*Ry0 + Rx4*Ry3 + Rx5*Ry6

Rxy4 = Rx3*Ry1 + Rx4*Ry4 + Rx5*Ry7

Rxy5 = Rx3*Ry2 + Rx4*Ry5 + Rx5*Ry8

Rxy6 = Rx6*Ry0 + Rx7*Ry3 + Rx8*Ry6

Rxy7 = Rx6*Ry1 + Rx7*Ry4 + Rx8*Ry7

Rxy8 = Rx6*Ry2 + Rx7*Ry5 + Rx8*Ry8

Ok sounds bad but it isn't really

Rxy0=cos(ay)

Rxy1=0

Rxy2=sin(ay)

Rxy3=sin(ax)*sin(ay)

Rxy4=cos(ax)

Rxy5=-sin(ax)*cos(ay)

Rxy6=-cos(ax)*sin(ay)

Rxy7=sin(ax)

Rxy8=cos(ax)*cos(ay)

where:

x = x*Rxy0 + y*0 + z*Rxy2 = x*Rxy0 + z*Rxy2

y = x*Rxy3 + y*Rxy4 + z*Rxy5

z = x*Rxy6 + y*Rxy7 + z*Rxy8

2-Axis matrix rotation takes: 8 mul, 5 add per point

The camera:

The rotation for the camera it's done in the same way, but we need to rotate the world around the negative camera's angles.

Just need to know that: cos(-a)=cos(a), sin(-a)=-sin(a)

cam_Rxy0=cos(ay)

cam_Rxy1=0

cam_Rxy2=-sin(ay)

cam_Rxy3=sin(ax)*sin(ay)

cam_Rxy4=cos(ax)

cam_Rxy5=sin(ax)*cos(ay)

cam_Rxy6=cos(ax)*sin(ay)

cam_Rxy7=-sin(ax)

cam_Rxy8=cos(ax)*cos(ay)

Here the matrix's way seems to be more complicated without gaining speed from it, but where u will facing 3-axis matrix rotation, then later use to describe on a 3d space an object or a camera a 4x4 matrix, it will be clear that the matrix's way is much better.

I suggest you to use and learn how to handle it.

Mutilate[OA]