We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Removing Objects from PApplet / Rotation of Object
Pages: 1 2 
Removing Objects from PApplet / Rotation of Object (Read 8901 times)
Re: Removing Objects from PApplet
Reply #15 - May 18th, 2010, 8:33am
 
in the XBox - class those three methods are important:

First, call this method. it takes 4 parameters:
current mouse coords x & y,
previous mouse coords x & y.

2D mouse coordinates are mapped to rotation in 3D.

method: freeRotation()
Code:

PVector v1 = new PVector();
PVector v2 = new PVector();
public void freeRotate(int mouseX, int mouseY,
                 int pMouseX, int pMouseY) {
long err;
err = vectorize(pMouseX, pMouseY, (int)this.x(), (int)this.y(), 800, v1);
err += vectorize(mouseX, mouseY, (int)this.x(), (int)this.y(), 800, v2);

if (err == 0) {
    Rot rotation = zeroHysteresisRotation(v1, v2);
    Rot prev_rotation = new Rot(RotOrder.XYZ, this.getRotArray()[0], this.getRotArray()[1], this.getRotArray()[2]);
    rotation = rotation.applyTo(prev_rotation);
    this.rotateTo(rotation.getAngles(RotOrder.XYZ));
}
}


method: zeroHysteresisRotation()
computes the rotation axis and angle. returns a shapes3d Rot object.
Code:
private Rot zeroHysteresisRotation(PVector v1, PVector v2 ) {
PVector cross;
float dot, angle;

dot = v1.dot(v2);

if (dot == 1.0)
return null; // nothing to do

cross = v1.cross(v2); // axis of rotation
cross.normalize();  

               // angle of rotation
angle = (float) (2.*Math.acos(dot));  

Rot rotationMatrix = new Rot(cross, angle);
return rotationMatrix;
}


method: vectorize()
compared to this code, we don't need to invert the y-axis.
Code:
private long vectorize(int mouseX, int mouseY, int originSpX, 
  int originSpY, int radius, PVector vec) {

float x,y,z, modulus;

x = (float)(mouseX - originSpX)/radius;
y = (float)(mouseY - originSpY)/radius;

modulus = x*x + y*y;
if (modulus > 1.) {
return 1L; // error
}

               // compute fictitious 'z' value
z = (float) Math.sqrt(1. - (x*x + y*y));    

vec.set(x,y,z);
return 0L;  
}

Re: Removing Objects from PApplet
Reply #16 - May 18th, 2010, 8:39am
 
I've got a new question now, also dealing with rotation. Wink

this time, i don't need to rotate an object around its own z-axis, but around the global processing canvas z-axis. no matter which rotation has been used before, no matter where the object is in the canvas, this should just rotate the object around the processing z-axis.
i already tried Code:
shapeOrientation() 

which didn't work.

how could I accomplish that? any help highly appreciated.
Re: Removing Objects from PApplet / Rotation of Object
Reply #17 - May 18th, 2010, 10:57am
 
The easiest way is a bit of a cheat but the Shapes3D library has a class called Anchor, it is not a visible component but can act as the parent for other shapes.
Assume you have a Box shape called box then
Code:

// Create an anchor object at the world origin(0,0,0)
// and with zero rotation.
Anchor anchor = new Anchor();
// Add the box
anchor.addShape(box);
// Rotate the anchor (rotateBy or rotateTo)
anchor.rotateBy(0,0,radians(30);

Although this would work I am not sure whether it would fit in with the rest of your code. Undecided

You might also try this
Code:
Rot rot = new Rot(new PVector(0,0,1), a);
PVector p = shape.getPosVec();
rot.applyTo(p);
shape.moveTo(p);

Where a is the angle of rotation round the z axis in radians.

This might work.

Re: Removing Objects from PApplet / Rotation of Object
Reply #18 - May 19th, 2010, 4:00am
 
unfortunately none of the solutions worked.
the first one doesn't work because the anchor is drawn at the world origin. so i translated it to the box coords, but I still don't get the right rotation axis. the anchor method is interesting though, i don't know why it doesn't work...
Re: Removing Objects from PApplet / Rotation of Object
Reply #19 - May 20th, 2010, 12:30am
 
Quote:
the anchor is drawn at the world origin. so i translated it to the box coords,

By moving the anchor to the same position as the box means that rotation of the anchor is the same as rotating the box itself. If we have 2 shapes s1 and s2 and then add s2 to s1, then rotation of s1 will cause s2 to rotate about the center of s1 and rotation of s2 will about the centre of rotation of s2. This can be seen in the Earth and Moon example that comes with the library.

As I said before the classes Rot and RotOrder are simple rewrites of the Rotation and RotationOrder classes from the Apache commons math project to make use of the PVector class rather than the immutable Vector3D class in that project so you might consider posting a new topic to see if anyone knows of any tutorials/examples using that project.

In a previous post I mentioned the shapeOrrientation method this allows the user to define a new Up vector and centre of rotation so you might try this.

Code:

// get current position
PVector p = shape.getPosVec();
// Move centre of rotation to new position (corVec)
// but leave Up vector unchanged
shape.shapeOrrientation(new PVector0,1,0), corVec);
// Rotate the shape
shape.rotateBy(...); // Rotate by whatever
// Reset the shapes centre of rotation.
shape.shapeOrrientation(new PVector0,1,0), p);


If this doesn't work I have no more ideas.

Re: Removing Objects from PApplet / Rotation of Object
Reply #20 - May 20th, 2010, 5:14pm
 
Thanks, but unfortunately it didn't work. Here's an answer from the Apache Commons Mailing List:

Quote:
I would suggest to compute the coordinates of the canvas Z axis in the
object frame and to rotate the object around this axis.
I assume you have some methods to get or set the orientation fo your
object with respect to the canvas. For the sake of the example, I will
assume the API is something like :
  Code:
Rotation r1 = myObject.getOrientation(); 


and
  Code:
myObject.setOrientation(r2); 



The assumed semantic is that v = r1(u) is such that if u is a vector in
object reference frame, v is the vector in the canvas reference frame,
i.e. myObject.getOrientation().applyTo(Vector3D.PLUS_K) gives the
orientation of the Z axis of the object in the canvas frame. What you
want is the opposite, so you have to revert the rotation.

You could use something along these lines :

Code:
 Vector3D zAxis =
   myObject.getOrientation().revert().applyTo(Vector3D.PLUS_K);


This vector is the canvas Z axis in the object frame. So if you apply a
rotation to the object around this axis, it will rotate around an axis
parallel to the canvas Z axis, but containing object origin.

If you don't have a getOrientation() method with the assumed semantic,
you will have to reconstruct the rotation by yourself. One way to do
this is to use the coordinates of two canonical axes of the object as
follows:

  Code:
public Rotation getOrientation() {

    // get the direction of object X axis with respect to canvas frame
    Vector 3D objectXAxis = ...;

    // get the direction of object Y axis with respect to canvas frame
    Vector 3D objectYAxis = ...;

    return new Rotation(Vector3D.PLUS_I, Vector3D.PLUS_J,
                        objectXAxis, objectYAxis);

}


Hope this helps,
Luc


Do you have an idea how to accomplish this within the shapes3d-domain? I haven't found this getOrientation() method. Or is there a way to retrieve the object's xAxis or yAxis (not the x,y coordinates) as proposed above?
Re: Removing Objects from PApplet / Rotation of Object
Reply #21 - May 21st, 2010, 2:31am
 
Since the classes Rot and RotOrder are simple rewrites of the Rotation and RotationOrder classes from the Apache commons math project to make use of the PVector class rather than the immutable Vector3D class it should be straightforward to translate the code from your last post to use the Rot and RotOrder classes in Shapes3D.

The only difficulty is the setOrrientation and getOrrientation methods because they work with Rotation objects rather than Cardan angles, but since you are extending the Box class then you can create your version of these methods in your class.

In Shapes3D the setOrrientaion method changes the XYZ coordinates of the actual shape by translation and rotation. This changes the Up vector and effectively the centre of rotation since the shape is no longer centred about (0,0,0) .
Re: Removing Objects from PApplet / Rotation of Object
Reply #22 - May 26th, 2010, 5:40am
 
Code:
public Rot getOrientation() {

    // get the direction of object X axis with
            // respect to canvas frame
            PVector objectXAx = new PVector(this.getRotVec().x, 0, 0);

    // get the direction of object Y axis with
            // respect to canvas frame
            PVector objectYAx = new PVector(0, this.getRotVec().y, 0);

    // transform global axis' into object axis'
    // translated to shapes3d
    return new Rot(VectorConstants.PLUS_I, VectorConstants.PLUS_J, objectXAxis, objectYAxis);

}


that's my rewritten code. I'm not sure about the new PVector() part at all. Is this right way to retrieve the object's x- and y-axis?

these are the code lines I use to perform the rotation:
Code:

public void rotateAroundGlobalZ(int direction) {

PVector zAxis = this.getOrientation().revert().applyTo(VectorConstants.PLUS_K);

Rot prev_rotation = new Rot(RotOrder.XYZ, this.getRotArray()[0], this.getRotArray()[1], this.getRotArray()[2]);
Rot rotation = new Rot(zAxis, (float) Math.toRadians(45));
rotation = rotation.applyTo(prev_rotation);

this.rotateTo(rotation.getAngles(RotOrder.XYZ));


... but this doesn't work either. the rotation is very jumpy and works only for a few times.
do you have an idea what could be wrong here? Thank you!
Re: Removing Objects from PApplet / Rotation of Object
Reply #23 - May 27th, 2010, 7:47am
 
I can't see any obvious errors in the code but in terms of manipulating rotations you have got to a point beyond my own understanding of using rotations, perhaps someone more knowledgeable in this area can help.

If there is anyone out there who is knowledgeable in this area please join in and give NEONLIGHT a hand.
Pages: 1 2