Author Topic: About rotate object  (Read 5697 times)

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
About rotate object
« on: March 25, 2009, 10:33:55 am »
Firstly ,excuse me for my english.

I want to rotate object in world by mouse.

use lwjgl.jar ,it has a method that can solve my problem
core code is
Code: [Select]
  int x=Mouse.getDX();
            int y=Mouse.getDY();
             if (Mouse.isButtonDown(0)) {
       
            // Rotate camera around object by holding down the left button
                SimpleVector line=new SimpleVector(x, 0, y);
                Matrix m=line.normalize().getRotationMatrix();
                m.rotateAxis(m.getXAxis(), (float) -Math.PI/2f);
                cam.moveCamera(Camera.CAMERA_MOVEIN, distance);
                cam.rotateAxis(m.invert3x3().getXAxis(), line.length() / 200f);
                cam.moveCamera(Camera.CAMERA_MOVEOUT, distance);
            }

   but i do not want to use this method ,how to solve my problem by  jpct ???

  thanks everyone .
« Last Edit: March 25, 2009, 10:43:42 am by alex »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About rotate object
« Reply #1 on: March 25, 2009, 10:42:00 am »
For accessing the mouse in an OpenGL window, you have to do it via LWJGL. There is nothing in jPCT that does this. It wouldn't be anything more than a simple wrapper around LWJGL's Mouse-class, so it would be pointless.

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: About rotate object
« Reply #2 on: March 25, 2009, 10:53:22 am »
I don't use lwjgl ,my code is
Code: [Select]
package org.test;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;

import com.threed.jpct.*;

import javax.swing.*;

public class MouseEventDemo {
private World world;
private FrameBuffer buffer;
private JFrame frame;
private TextureManager texMan = null;
private static float distance = 70;

Canvas canvas;

public static void main(String[] args) throws Exception {
MouseEventDemo d=new MouseEventDemo();
d.loop();
}

public MouseEventDemo() throws Exception {
Config.fadeoutLight = true;
Config.maxPolysVisible = 1000000;

frame = new JFrame("MouseTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());


world = new World();
world.setAmbientLight(211, 211, 211);
addTexture();
load3dFile();
world.getCamera().lookAt(new SimpleVector(0, 0, 0));
}

private void loop() throws Exception {
buffer = new FrameBuffer(600, 600, FrameBuffer.SAMPLINGMODE_NORMAL);

canvas = buffer.enableGLCanvasRenderer();
canvas.addMouseMotionListener(new MyMouseMotionListener(this));

frame.getContentPane().add(canvas,BorderLayout.CENTER);
frame.setSize(600, 620);
frame.setVisible(true);


while (frame.isShowing()) {
buffer.clear(java.awt.Color.LIGHT_GRAY);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
buffer.display(canvas.getGraphics());
Thread.sleep(10);
}
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.dispose();
frame.dispose();
System.exit(0);
}

public void addTexture() {
texMan = TextureManager.getInstance();
String pathTextura = "impreza/";
File dir = new File(pathTextura);
String texturas[] = dir.list();
for (int i = 0; i < texturas.length; i++) {
if (texturas[i].endsWith(".JPG"))
texMan.addTexture(texturas[i].toUpperCase(), new Texture(
pathTextura + texturas[i]));
}
}

public void load3dFile() {
Object3D[] cars = Loader.loadOBJ("impreza/impreza.obj",
"impreza/impreza.mtl", 2f);
for (Object3D part : cars) {

part.build();
world.getCamera().setPosition(5, 15, 10);
world.addObject(part);
}
}
public void mouse_dragged(MouseEvent e) {
float x= world.getCamera().getDirection().x;
float y= world.getCamera().getDirection().y;
  SimpleVector line=new SimpleVector(x, 0, y);
          Matrix m=line.normalize().getRotationMatrix();
          m.rotateAxis(m.getXAxis(), (float) -Math.PI/2f);
          world.getCamera().moveCamera(Camera.CAMERA_MOVEIN, distance);
          world.getCamera().rotateAxis(m.invert3x3().getXAxis(), line.length() / 200f);
          world.getCamera().moveCamera(Camera.CAMERA_MOVEOUT, distance);
}
}

class MyMouseMotionListener implements MouseMotionListener {
MouseEventDemo demo;
MyMouseMotionListener(MouseEventDemo demo) {
this.demo = demo;
}
public void mouseDragged(MouseEvent e) {
demo.mouse_dragged(e);
}
public void mouseMoved(MouseEvent e) {

}

}
how to rotate object? i.e how to rotate object by rotating camera?Do I have to use lwjgl?
« Last Edit: March 25, 2009, 10:59:49 am by alex »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About rotate object
« Reply #3 on: March 25, 2009, 11:07:32 am »
I see...no, in that case (you are not rendering into a native LWJGL window), you can't use LWJGL. What you need to use a method similar to the one using LWJGL is basically a substitute for LWJGL's

Code: [Select]
  int x=Mouse.getDX();
  int y=Mouse.getDY();

which gives you the number of pixels the mouse has moved in x and y direction since the last frame. You can easily create something like that out of the position value that an AWT mouse listener returns.


Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: About rotate object
« Reply #4 on: March 25, 2009, 11:11:34 am »
thanks a lot.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: About rotate object
« Reply #5 on: March 25, 2009, 12:44:06 pm »
I usually use a setup similar to this one:

Code: [Select]
// imports needed:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

...

// Make your applet/application a MouseListener and MousMotionListener:
... implements MouseListener, MouseMotionListener

...

// Global varriables needed:
    private int prevMouseX, prevMouseY;

...

// Make the canvas and applet/application receive mouse input:
        // receive mouse input from the main applet/application:
        addMouseListener( this );
        addMouseMotionListener( this );

        // also get mouse input picked up by the canvas:
        myCanvas.addMouseListener( this );
        myCanvas.addMouseMotionListener( this );

...

// Mouse methods needed:
    public void mouseEntered( MouseEvent e ) {}
    public void mouseExited( MouseEvent e ) {}
    public void mouseMoved( MouseEvent e ) {}
    public void mouseReleased( MouseEvent e ){}
    public void mouseClicked( MouseEvent e ){}
    // Dragging the mouse should rotate the entire gear assembly
    public void mouseDragged( MouseEvent e )
    {
        // get the mouse's coordinates:
        int x = e.getX();
        int y = e.getY();
        Dimension size = e.getComponent().getSize();

        // Calculate the angles to rotate the object:
        float thetaY = (float)Math.PI * ( (float)(x-prevMouseX)/(float)size.width );
        float thetaX = (float)Math.PI * ( (float)(prevMouseY-y)/(float)size.height );
       
        // Apply the rotations to the object:
        myObject.rotateX( thetaX );
        myObject.rotateY( thetaY );

        // Keep track of the mouse location:
        prevMouseX = x;
        prevMouseY = y;
    }
    public void mousePressed( MouseEvent e )
    {
        // Start keeping track of the mouse location now.
        // This prevent the object from jerking to the new angle
        // whenever mouseDragged first gets called:
        prevMouseX = e.getX();
        prevMouseY = e.getY();
    }

You might need to play around with the rotation axis' to get your object to ratate the directions you want it to, but this will at least give you something to start with.

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: About rotate object
« Reply #6 on: March 25, 2009, 01:31:25 pm »
Really helps to read these comments because I didn't know lwjgl had mouse events.
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About rotate object
« Reply #7 on: March 25, 2009, 03:11:37 pm »
Really helps to read these comments because I didn't know lwjgl had mouse events.
Not events in the AWT sense. You can "just" poll the mouse.

Offline alex

  • byte
  • *
  • Posts: 19
    • View Profile
Re: About rotate object
« Reply #8 on: March 26, 2009, 09:50:16 am »
thank you ,paulscode。
I have solved this problem .

Originally ,I wanted to  achieve the  effect of rotating whole object (there are many objects in world  ) through  rotating  camera .but i can't .So i change my train of thought ,i use setparent()(Object3D class) for adding all object in a new object ,then i try to rotate the new object when i drag my mouse.