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 8902 times)
Removing Objects from PApplet / Rotation of Object
Apr 27th, 2010, 1:34am
 
Hey all,
I have a question concerning removing shapes from the current PApplet. We're working with a library called shapes3d (link here: http://www.lagers.org.uk/s3d4p/distribution/web/reference/)

Currently, we store all our shapes (which are boxes in an ArrayList). We implemented a reset method, that should erase all current shapes and replace them with new ones. Unfortunately, it's not enough to just remove the objects from the ArrayList, they're still in the PApplet. They're not visible by any means, but you can't select objects behind those invisible ones.

Is there any way to get rid of shapes you don't need anymore Do I have to set them to NULL, or is there any other way to remove them
Thanks in advance for an answer. We really need help on this one... Sad
Re: Removing Objects from PApplet
Reply #1 - Apr 27th, 2010, 11:15am
 
The Shapes3D library maintains a collection of all shapes created so even if you clear() the ArrayList they are still referenced and so are not removed by the garbage collection.

This is an essential part of the library since this collection is used to enable 3D shape picking.

To get rid of the shape they need to be removed from this collection. To do this there is a method called finshedWith()

So to reset your ArrayList first iterate over the list and call this method for each shape then clear the list e.g.

Code:

for(int i = 0; i < mLlist.size(); i++){
 Shape3D s = (Shape3D)myList.get(i);
 s.finishedWith();
}
myList.clear();



I think this method was introduced in V1.7.0 which I released this morning so I suggest you download it.

Will be interested in seeing what you create with this library.

Smiley
Re: Removing Objects from PApplet
Reply #2 - Apr 28th, 2010, 6:08am
 
thank you for the prompt reply, it works now!!
i have another question now Wink

as for the project: we're doing a prototype for an evaluation environment which compares different wii interaction methods.
we're using your library because it's so convenient to pick and to put textures on the shapes. if you're interested: here's the link to our project: http://code.google.com/p/3d-media-browser/

as of now, we have another big issue. we have different shapes in our canvas, and we want to be able to provide a user friendly approach to rotate shapes with the mouse. for this purpose, we use the rotateBy() methods provided by the Shape3D class.

right now, our approach is kinda buggy. we use our own shape class which inherits from the shapes3d.Box class. in some cases, rotation around the x- and z- axis seem to do the same thing, which normally would'nt be possible. it is important to provide a mouseDragged-rotation - right now this is messy. do you have any advice, which rotation method would be best for us
Re: Removing Objects from PApplet
Reply #3 - Apr 28th, 2010, 12:01pm
 
I suspect that you might be getting gimbal lock which is possible when using Euler angles to define rotation.

The above link seems to suggest changing the order of rotation to YZX rather than the order XYZ that Shapes3D uses might solve the problem, I am not convinced but is worth trying.

Copy the following code into your XBox class, it will override the method in the Box class but now does the rotation in YZX order.

Please, please post a reply when you have tried this if it works I need to think about modifying the library.

Looking forward to hearing from you.
Smiley

Code:

public void draw() {
app.pushStyle();
app.pushMatrix();
if(pickModeOn){
drawForPicker(pickBuffer);
}
else {
app.translate(pos.x, pos.y, pos.z);
app.rotateY(rot.y);
app.rotateZ(rot.z);
app.rotateX(rot.x);
app.scale(shapeScale);

if(visible){
switch(drawMode){
case TEXTURE:
case SOLID:
drawColorTexture();
break;
case WIRE:
drawWireFrame();
break;
}
}
if(children != null){
Iterator<Shape3D> iter = children.iterator();
while(iter.hasNext())
iter.next().draw();
}
}
app.popMatrix();
app.popStyle();
}
Re: Removing Objects from PApplet
Reply #4 - May 15th, 2010, 6:28am
 
thank you very much. unfortunately this didn't work for me. I'm trying to figure out how quaternions work at the moment. since you have implemented your rotations with quaternions could you help me out with some good literature?
Re: Removing Objects from PApplet
Reply #5 - May 15th, 2010, 9:22am
 
Quote:
could you help me out with some good literature?


Not really my area - yes I used quaternions but the Rot class in Shapes3D was adapted by me from the Apache commons math project to work with the PVector class.

It is not so important to understand the maths behind quaternions but how to use them to perform the desired actions.

Perhaps someone else could suggest a good primer as I would also be interested.

Re: Removing Objects from PApplet
Reply #6 - May 15th, 2010, 10:54am
 
ok, i tried to get it to work by using euler-angles. of course, i run into gimbal locks. i now know which cases are buggy.

here's the link to our project: http://code.google.com/p/3d-media-browser/

in Input.java, the mouseX and mouseY coordinates are mapped to a rotation. this doesn't work well as of now. do i use the RotOrder class provided in the shapes3d library the right way?

Code:

line 405 (Input.java):
if (checkY>=FIRST_EIGHTH&&checkY<THIRD_EIGHTH) {
float[] angles = new Rot(RotOrder.XZY, radians(dy), radians(dx), 0).getAngles(RotOrder.XZY);
selectedBox.rotateXYZ(0, 0, 0, angles[0], angles[1], angles[2]);
}


this line gives me a gimbal lock and i have no idea how to prevent it by setting another Rotation order.  Any advice? I'm stuck...  Undecided
Re: Removing Objects from PApplet
Reply #7 - May 16th, 2010, 7:48am
 
First I would split the instruction into two parts for the sake of the discussion.i.e.
Code:

Rot rot = new Rot(RotOrder.XZY, radians(dy), radians(dx), 0);
float[] angles = rot.getAngles(RotOrder.XZY);



The RotOrder class defines the order that rotations are to be applied so
Code:
new Rot(RotOrder.XZY, a1, a2, a3) 



defines the following rotation sequence
(1) a1 radians about the 1,0,0 axis i.e. X
(2) a2 radians about the 0,0,1 axis i.e. Z
(3) a3 radians about the 0,1,0 axis i.e. Y

Now according to the writers of the Apache commons maths library Gimbal lock occurs when the second angle in the sequence i.e. (2) is -90o or +90o so I suspect you must pick your order carefully.

the getAngles method then returns an array of 3 angles based on the order they are to be applied. Since Shapes3D applies rotations in XYZ order you might try that order when getting the angles.
Re: Removing Objects from PApplet
Reply #8 - May 17th, 2010, 5:03am
 
Again, thank you very much for the support.
I try a different approach now, since the Euler/Cardan angles based rotation doesn't work the way I need it.

I found this very useful explanation: http://www.mactech.com/articles/mactech/Vol.15/15.03/NaturalObjectRotation/
Basically, it uses vectors and matrix multiplications for the rotation. I translated the code to Java, but I need a method like this.

Code:
setRotateAboutAxis(Point origin, Vector3D axis, float angle) 


to define my own axis of rotation. Later, I should be able multiply that with the object's current rotation. is there anything like this in the shapes3d library Haven't found it yet.  Undecided

Here's another link to the method, provided in QuickTime:
Link to API

Re: Removing Objects from PAppletis there anything lik
Reply #9 - May 17th, 2010, 5:58am
 
The Shapes3D library effectively uses the Apache commons maths project, all I did was translate it to work with PVector. So anything which can be done in the Apache project can be done in Shapes3D (at least for rotations).

I have browsed the mactech article with interest and I suspect you can do the same thing using the Rot class.

Step 1 would be to calculate the 2 3D vectors based on the mouse movement using the imaginary sphere. Lets call these v1 and v2 create a Rot object with
Code:
rot = new Rot(v1, v2); 


This will define the rotation required to move from v1 to v2 now if we have another vector pv1 and we want to apply the same rotation then this can be done with
Code:
rot.applyTo(pv1); 


If you want to combine rotations there is a version of applyTo that accepts a Rot object as a parameter.
It might be worth creating a simpler sketch where you can experiment with the Rot class.
I have enough problems spelling quaternions never mind understanding the math. Perhaps someone more knowledgeable about Apache commons math / 3D rotations could help here as I would be interested as well.

You might also delve into the PeasyCam source as this uses the Apache library as well.
Smiley
Re: Removing Objects from PApplet
Reply #10 - May 17th, 2010, 6:12am
 
Ok, thanks! I guess that solves the rotation problem before I apply it to the shape. How can I apply an instance of Rotation to a Shape3d object WITHOUT using cardan/euler angles? i wish there was something like:
Code:
rotation.applyTo(my_shape) 



because if I use something like this instead, i can't get rid off the gimbal lock:
Code:
my_shape.rotateTo(rotation.getRotVec()) 


Re: Removing Objects from PApplet
Reply #11 - May 17th, 2010, 7:18am
 
I don't know if this will work but you might try it.

In Shapes3D There is a method called
Code:
public void shapeOrientation(PVector up, PVector centreOfRot){


Now the up vector would normally be (0,1,0) so if you calculate the rot as per previous post then

Code:
pv1 = new PVector(0,1,0);
rot.applyTo(pv1);


This should calculate the new up vector for the shape then
Code:
myShape.shapeOrrientation(pv1, new PVector); 


might do the trick but it is a guess.
Roll Eyes
Re: Removing Objects from PApplet
Reply #12 - May 17th, 2010, 9:32am
 
i solved it now!  Smiley thanks for all the help!
what i did was basically the following (based on this code: link)
Code:

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));


most important thing is to let the Rot class do the job. convert both the previous and the changed rotation to quaternions (use new Rot()), apply one rotation to another and use the resulting rotation matrix for object's rotation. no gimbal lock whatsoever!
this did the job!!! Smiley perfectly working now! thanks again!
Re: Removing Objects from PApplet
Reply #13 - May 17th, 2010, 11:21am
 
That's great could you post your Processing code for the zeroHysteresisRotation as I will be able to make use of it in later versions of the Shapes3D library (post 1.8)

Thanks in advance.
Re: Removing Objects from PApplet
Reply #14 - May 18th, 2010, 3:59am
 
hey there!
thanks for all the help, our source code is entirely online over here: http://code.google.com/p/3d-media-browser/.
Pages: 1 2