Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (756) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 rotation question  (Read 2981 times) 0 Members and 1 Guest are viewing this topic.
Zorqu

Senior Newbie

Java games rock!

 « Posted 2004-11-11 08:01:54 »

Hi, I have what is probably a simple question and I'm learning Xith3D to use to create a game so expect many more!

I'm wondering how I can use the rotate function to rotate around the Z axis at specified X and Y coords.  Instead of always rotating around (0,0).  Sorry if this is in the docs somewhere I must have missed it, thanks!

Ok, I have another question already, its along the same lines so I'll post it here as well:

I have my event listener set up already, and I'm wondering how I can get the X and Y coords of where the user clicks.  I got the mouse coords using e.getX() and e.getY() however that doesn't help me I need the X,Y coords on my game map.  Thanks again.
LaLiLuLeLo

Senior Newbie

Foooocus!

 « Reply #1 - Posted 2004-11-11 13:10:57 »

To rotate around a particular axis, this is one way.
 1 hero.getTransform().setRotation(new AxisAngle4f(new Vector3f(0, 0, 1), 3.14f))

It sounds to me like you have two transform3d nodes (one for rotation and one for translation) and that you have their positions in the scenegraph mixed up.

To convert mouse coords to world coords, you might need to consider that the (0,0) is at the top left of the canvas for the mouse and (0,0,0) is at the center. You can always do a hack conversion like this "(e.getX() - canvas.getWidth()/2)/3;" till you can figure out the trig to get the correct values of depth
Zorqu

Senior Newbie

Java games rock!

 « Reply #2 - Posted 2004-11-12 07:39:02 »

Quote
To rotate around a particular axis, this is one way.
 1 hero.getTransform().setRotation(new AxisAngle4f(new Vector3f(0, 0, 1), 3.14f))

It sounds to me like you have two transform3d nodes (one for rotation and one for translation) and that you have their positions in the scenegraph mixed up.

To convert mouse coords to world coords, you might need to consider that the (0,0) is at the top left of the canvas for the mouse and (0,0,0) is at the center. You can always do a hack conversion like this "(e.getX() - canvas.getWidth()/2)/3;" till you can figure out the trig to get the correct values of depth

Thank you for the reply, however I still can't seem to get it working the way I want.  What is the "hero" in that line of code?

Your saying the best way to get the X,Y coords in world is to convert from mouse coords using math?  I'll play with that idea some more and see if I can figure something out.

Here is my code so you get a better idea of what I'm trying to do, I want to rotate around viewX and viewY and it will only rotate around 0,0.  If you notice any other problems, please comment.  Thanks!

DonCrudelis

Junior Devvie

 « Reply #3 - Posted 2004-11-12 10:28:45 »

You should make a Transform3D for the position
and another Transform3D where you put the rotation,
then you should write:

 1  2  3  4  5 Transform3D position = new Transform3D();   // create new Transform3DTransform3D rotation = new Transform3D();   // create new Transform3Drotation.rotY(45);   // rotate Y 45 degreesrotation.setTranslation(new Vector3f(100, 100, 100));   // rotation axisposition.mul(rotation);   // combining the both

This works for me, and the "new Vector3f(100, 100, 100)" is the position where you want your object to rotate around.

If you want your object to rotate around itself:
 1  2  3  4  5 Transform3D position = new Transform3D();   // create new Transform3Dposition.setTranslation(new Vector3f(100, 100, 100));Transform3D rotation = new Transform3D();   // create new Transform3Drotation.rotY(45);   // rotate Y 45 degreesposition.mul(rotation);   // combining the both

Where "new Vector3f(100, 100, 100)" is just the position of your object.

Good luck!

Alonzo
DonCrudelis

Junior Devvie

 « Reply #4 - Posted 2004-11-12 10:34:52 »

Also, to get a mouse look, you can do like this:

 1  2  3  4  5  6  7  8 Transform3D position = new Transform3D();Transform3D rotationX = new Transform3D();Transform3D rotationY = new Transform3D();rotationX.rotX(45);  // x anglerotationY.mul(rotationX);rotationY.rotY(45);  // y angleposition.setTranslation(new Vector3f(10, 10, 10));  // the positionposition.mul(rotationY);

(It will look much better)

Good luck!

Alonzo
rmdire

Junior Devvie

 « Reply #5 - Posted 2004-11-25 20:06:57 »

Hi,

How can I get my camera to rotate around a point - say Vector3f(200f,0.0,10.0f ) around the Y Axis.
using View.getTransform() and still looking at that point
and maintaining the same distance from the point.
I've been trying to figure this out and I just can't seem to get it.

I have tried:
Vector3f v = new Vector3f();
Object.getTransform().get(v);
Transform3D position = new Transform3D();
position.setTranslation(v);
Transform3D rotation = new Transform3D();
rotation.rotY(angle);              position.mul(rotation);

camView.setTransform(position);

LaLiLuLeLo

Senior Newbie

Foooocus!

 « Reply #6 - Posted 2004-11-29 12:39:01 »

Maybe you could try the lookAt function
 1  2  3 view.getTransform().lookAt(      new Vector3f(0, 120.0f, 1.0f), //Camera location                                                      new Vector3f(0, 0, 0), //COV                                                      new Vector3f(0, 1, 0)); //Up Vector

jaakko777

Senior Newbie

 « Reply #7 - Posted 2006-01-27 16:20:38 »

Dunno if this thread is still "alive" but there is a neat function i made for doing that.
It rotates about a Node but you can easily extract the relevant parts of it to suit your needs.

public BrachGroup rootBG; // <-- The root of your scenegraph...

/**
* Rotates the camera about the selected node.
*
* If <code>node</code> is null then rotation will be performed about the VirtualWorld origo.
* Direction of the rotation is taken from <code>x</code> and <code>y</code>.
*
* @param camera
* @param node
* @param x
* @param y
*/
public void rotateCameraAboutNode(View camera, Node node, float x, float y) {
Transform3D rotationLocation = new Transform3D();
Transform3D cameraLocation = new Transform3D();
Transform3D traslateLocation = new Transform3D();
Matrix3f rotation1 = new Matrix3f();
Matrix3f rotation2 = new Matrix3f();

// Determine camera location
camera.getTransform(cameraLocation);

// Determine rotation point (node or vworld)
if (node == null) {
// for rotating about origo this works
rootBG.getLocalToVworld(rotationLocation); // <-- rootBG is your scenegraph root

// rotate
rotation1.setIdentity();
rotation2.rotX(y / 100.0f);
rotation1.mul(rotation2);
rotation2.rotY(x / 100.0f);
rotation1.mul(rotation2);
rotationLocation.setRotation(rotation1);
rotationLocation.mul(cameraLocation);
// set new camera transform
cameraLocation.set(rotationLocation);
} else {
// for rotating about something else than origo we need slight
// modifications to rotation code
rootBG.getLocalToVworld(rotationLocation);
node.getLocalToVworld(traslateLocation);

// translate to rotation point
rotationLocation.mul(traslateLocation);
// rotate
rotation1.setIdentity();
rotation2.rotX(y / 100.0f);
rotation1.mul(rotation2);
rotation2.rotY(x / 100.0f);
rotation1.mul(rotation2);
rotation1.normalize();
rotationLocation.setRotation(rotation1);
// translate away from rotation point
traslateLocation.invert();
rotationLocation.mul(traslateLocation);
rotationLocation.mul(cameraLocation);
// set new camera transform
cameraLocation.set(rotationLocation);
}

// In Java3D the transform sometimes got non-congruent but that hasnt happened in Xith3D.. Is it normalized in the xith core
// the transform gets non-congruent so often that we might as well
// normalize it every time
// cameraLocation.normalize();

try {
camera.setTransform(cameraLocation);
} catch (Exception e) {
e.printStackTrace();
}
}

Note that this rotation will be applied as if the x and y parameters were in the node's coordinate space.
I guess it would require a quaternion rotation to get it work right in all cases, but that is something that is still beyond my comprehension...
Pages: [1]
 ignore  |  Print

 DesertCoockie (52 views) 2018-05-13 18:23:11 nelsongames (83 views) 2018-04-24 18:15:36 nelsongames (74 views) 2018-04-24 18:14:32 ivj94 (759 views) 2018-03-24 14:47:39 ivj94 (87 views) 2018-03-24 14:46:31 ivj94 (643 views) 2018-03-24 14:43:53 Solater (102 views) 2018-03-17 05:04:08 nelsongames (184 views) 2018-03-05 17:56:34 Gornova (426 views) 2018-03-02 22:15:33 buddyBro (1086 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org 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‑gaming.org