Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Camera Rotation  (Read 1985 times)
0 Members and 1 Guest are viewing this topic.
Offline Preacher

Junior Devvie

Java games rock!

« Posted 2004-01-26 10:36:49 »

Hi trying to get my head around the mechanics of how to rotate and trasform a camera usgin xith.

can't find many tutorials on it and got some code from a few posts in this forum but still i don't really understand about multipling the matrices.

I want to be able to move tha camera like in the game Black and White which im sure ye all know, so basically the camera is above the chess board (making a chess game) with its view origin towards the center of the board. when the user moves mouse to the left or right edge of the screen and clicks and drags the camera will rotate around the central axis of the board.

likewise with the top and bottom edge but the camera will tilt around the central axis of the board. also want the camera to zoom in and out from the board using the mouse wheel.

hope thats a good description of what i want to do, sorry about the none tecnical language. I dont mind doing the research myself just can't find any good tutorials on doing this sort of thing in xith or jogl, so if anyone can suggest code or web sites that would be great Smiley
Offline tom
« Reply #1 - Posted 2004-01-26 13:12:36 »

Here is the code I'm using that does what you described:
// these are global vars
float xrot = 0; // rotation around the right axis
float yrot = 0; // rotation around the up axis
float distance = 250; // the distance of the camera from the origin

// this goes in the game loop
// (Mouse.dx, Mouse.dy) is the mouse movement since last frame
if (Mouse.isButtonDown(0)) {
  // update rotation when left mouse button is dragged
  xrot += Mouse.dy*0.01f;
  float max = (float)(Math.PI/2.0*0.9);
  xrot = Math.min(max, Math.max(-max, xrot));
  yrot += Mouse.dx*0.01f;
} else if (Mouse.isButtonDown(1)) {
  // update distance when right mouse button is dragged
  distance -= distance * Mouse.dy/100;

// calculate position of camera using cos and sin
float cos = (float)Math.cos(xrot);
float x = cos*(float)Math.cos(yrot) * distance;
float y =     (float)Math.sin(xrot) * distance;
float z = cos*(float)Math.sin(yrot) * distance;

// feed the position to the lookAt function. We are looking at the center of the world
GLU.gluLookAt(x, y, z, 0, 0, 0, 0, 1, 0);

Just replace GLU.gluLookAt with view.getTransform().lookAt()

It is possible to extend this so that also can move on the viewing plane. That is move right, left, up and down. But it's a little bit more complicated. But I can give you the details if you need it.

Offline Preacher

Junior Devvie

Java games rock!

« Reply #2 - Posted 2004-01-26 13:16:17 »

I think thats all i need i'll try it out tonight thanks very much Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aNt

Senior Devvie


« Reply #3 - Posted 2004-01-27 04:58:01 »

tom tom tom go for it Smiley
Pages: [1]
  ignore  |  Print  

EgonOlsen (76 views)
2018-06-10 19:43:48

EgonOlsen (56 views)
2018-06-10 19:43:44

EgonOlsen (76 views)
2018-06-10 19:43:20

DesertCoockie (256 views)
2018-05-13 18:23:11

nelsongames (156 views)
2018-04-24 18:15:36

nelsongames (155 views)
2018-04-24 18:14:32

ivj94 (896 views)
2018-03-24 14:47:39

ivj94 (157 views)
2018-03-24 14:46:31

ivj94 (809 views)
2018-03-24 14:43:53

Solater (173 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!