Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - urubu

#1
I did some more testing and now I am using -Float.MAX_VALUE instead of Float.MIN_VALUE. I am using:

float minX = Float.MAX_VALUE, minY = Float.MAX_VALUE, minZ = Float.MAX_VALUE, maxX = -Float.MAX_VALUE, maxY = -Float.MAX_VALUE, maxZ = -Float.MAX_VALUE;

Instead of:

float minX = Float.MAX_VALUE, minY = Float.MAX_VALUE, minZ = Float.MAX_VALUE, maxX = Float.MIN_VALUE, maxY = Float.MIN_VALUE, maxZ = Float.MIN_VALUE;


This has fixed my problem. I suggest the wiki to be updated.
#2
I just spotted something that does not sound right.
maxX,maxY,maxZ are initialized with Float.MIN_VALUE. This is defined as the smallest positive float number and the logic of the method does not seem to work if the coordinates have negative numbers.

I did a quick test changing those values to be -150000 and then I always got the expected results.
#3
I have an object3d that is a cube initially set at (0,0,0).
When I invoke getWorldSpaceBounds for this cube I get (0,0,0) and (1,1,1) that I think is the expected result.

I then do a lot of rotations and translation on this cube. Each individual rotation is always 180dg or -180dg around one of the axis so that the cube local coordinate axis are kept parallel to the world axis (the sides of the cube are always parallel to the screen)

I then calculate the size of the sides of the returned bounding box (using the world bounds) using w=maxX-minX, h=maxY-minY, d=maxZ-minZ. I would expect this to always be 1,1,1 and it is most of the time. The problem is that after some rotations/translations I begin to get strange values for w,h,d like 3,2,1.

Am I missing anything?



#4
does not work reliably though.
Sometimes  I get strange big numbers for the coordinates.
Before I call that method I check for collision with the object. To do that I have to offset the y position 77 pixels (don't know why, got it by experimentation). May be that is confusing the calculations.
#5
I guess I figured that out mtself:

public int calcFlingDirection(PointF start, PointF finish,
                                     FrameBuffer fb, Object3D selCube, World world) {
        Camera cam=world.getCamera();
        // Convert start into a direction vector in world space:
        SimpleVector direction1 = new SimpleVector( Interact2D.reproject2D3DWS(
                                                       cam, fb, (int)start.x, (int) start.y ) ).normalize();
        // Convert finish into a direction vector in world space:
        SimpleVector direction2 = new SimpleVector( Interact2D.reproject2D3DWS(
                                                       cam, fb,(int) finish.x, (int) finish.y ) ).normalize();

        // Calculate the distance to whatever was clicked on:
        SimpleVector pos=cam.getCamera().getPosition();
     
        float distance1 = world.calcMinDistance( pos, direction1, 10000 );
       
        float distance2 = world.calcMinDistance( pos, direction2, 10000 );

        // Calculate the 3D coordinates for the point that was clicked in world coordinates:
        SimpleVector collisionPoint1 = new SimpleVector( direction1 );
        collisionPoint1.scalarMul( distance1 );
        collisionPoint1.add( cam.getCamera().getPosition() );
        collisionPoint1.matMul(selCube.getInverseWorldTransformation());
       
        SimpleVector collisionPoint2 = new SimpleVector( direction2 );
        collisionPoint2.scalarMul( distance2 );
        collisionPoint2.add( cam.getCamera().getPosition() );
        collisionPoint2.matMul(selCube.getInverseWorldTransformation());
        collisionPoint2=round(collisionPoint2);

        SimpleVector diff=new SimpleVector(collisionPoint2);       
        diff.calcSub(collisionPoint1);
        int res='x';
        if (diff.x<0)
        res=-res;
       
        float x2=(diff.x>0 ? diff.x : - diff.x);
        float y2=(diff.y>0 ? diff.y : - diff.y);
        float z2=(diff.z>0 ? diff.z : - diff.z);

        if (y2>x2) {
        res='y';
        if (diff.y<0)
        res=-res;
        }
        if ( (z2>y2) && (z2>x2)) {
        res='z';
        if (diff.z<0)
        res=-res;
        }
        return res;
}



The above code returns 'x',-'x','y',-'y','z',-'z' in local coordinates (cube coordinates)
This is working fine on my tests. Please notice that both points MUST collide with the cube surface.
#6
Is it possible to identify the direction of a swipe in relation to an object?
I have attached an image to help clarify what I need. I have a cube and I would like to be able to identify which edge of the cube the swipe move is targeting.
I am currently detecting the 2d coordinates of first and last touch on the screen but I don;t know how to translate that to local coordinates of the cube.
I would think that if I have the two points in local coordinates I could infer the direction of the movement.
Any thoughts?

[attachment deleted by admin]
#7
Support / Re: rotate around edge
April 19, 2012, 02:08:15 PM
Adding those calls made it work!

I know it may not be the right solution but getting things working the way you expect (after weeks of trial and error)  is a very pleasant feeling and I can't resist the temptation of using even a  non recommended solution :-).

I think the problem that continues to be unsolved for me (without doing the above hack)  is how do I rotate an object (a cube in my case) using a pivot that is parallel to one of the axis once the object has been rotated.
Not sure if I am the only one interested in this but a good tutorial on rotateAxis() would be very helpful.

Thank you very much for your help.
#8
Support / Re: rotate around edge
April 19, 2012, 11:52:20 AM
Quote from: EgonOlsen on April 08, 2012, 09:14:46 PM
Rotations are cummulative, but the rotation pivot isn't. I.e. if you rotate 90° around z with a pivot of (1,0,0) and you then change the pivot to (-1,0,0), the result will be different. That's why you are getting this "jump". In your case, you might want to consider to use the rotateAxis()-method instead with the axis being the edge you want to rotate around. You have to calculate that edge based on your object's rotation matrix though.

How would I calculate the edge? I tried everything I could but I am unable to get that to do what I want. I was able to make it work on the desktop version by using this code:

private void rotateEdge(SimpleVector pivot, float angle, char axis) {
   cube.setRotationPivot(pivot);
   switch(axis) {                     

case 'x':
cube.rotateX(angle);
break;
case 'y':
cube.rotateY(angle);
break;
case 'z':
cube.rotateZ(angle);
break;
}
    cube.rotateMesh();
    cube.clearRotation();
}


In other words if I rotate the mesh and clear the rotation I could specify the pivot I wanted every time and it worked.

I would be ok with that solution (I don't need many rotations) but the thing is that it does not work on jpct-ae. On jpct-ae if I clear the rotation even after rotating the mesh nothing happens to the cube.

I am totally lost...
#9
Support / Re: rotate around edge
April 08, 2012, 01:34:40 PM
Quote from: EgonOlsen on April 05, 2012, 08:58:45 PM
The rotation axes are in object space. Object space isn't affected by any rotations of the object, so if you rotate the cube in a way that it ends up below your rotation axis, what was formerly a rotation around it's lower edge will become a rotation around it's upper edge.

My understanding of your explanation (and that may be where I am misunderstanding the concept) is that if I specify a pivot that is an edge it will always rotate around that edge. It may be upside-down and give the impression it is rotating in a lower (or upper) edge but it would always rotate around the same edge in terms of local coordinate.

Using the test code I sent in the previous post I noticed that that was true for some edges but not for all of them. Using that test code, if I  set the pivot to (0,0,1) and rotateX the cube rotates around the upper back edge as expected.

On the other hand, when I execute the steps below, things do not work as I expected. Here are the steps and what happens:

  • Set the pivot to (0,1,0) and rotate 180 degrees around Z. This works as expected. The cube is rotated around the left lower edge and ends up upside-down
  • On top of the previous rotation, set the pivot now to (0,0,1) and rotate around X. I would expect the cube to be rotated around the same edge as before but what happens is that the cube "jumps" (I think it gets translated somehow) to (0,0,0) and rotates around the edge there.

Please notice that I am using jpct-ae and that all vectors are considering the cubes created using the test code I posted.
#10
Support / Re: rotate around edge
April 04, 2012, 09:36:46 PM
Here is the code I am using. Rotate the top pink box to the left (using the up arrow key) until it is at the same height as the green bottom box (PI rotation on Z). Then (using the down arrow key)  rotate it to the back (X rotation) reproduces the behaviour I described.

package com.cubetest.example;

import java.lang.reflect.Field;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;

/**

*/
public class HelloWorld extends Activity {

// Used to handle pause and resume...
private static HelloWorld master = null;

private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
private World world = null;
private RGBColor back = new RGBColor(50, 50, 100);
private RGBColor c1 = new RGBColor(50, 100, 50);
private RGBColor c2 = new RGBColor(100, 50, 50);
private Object3D cb=null;


protected void onCreate(Bundle savedInstanceState) {

Logger.log("onCreate");

if (master != null) {
copy(master);
}

super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());

mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
// back to Pixelflinger on some device (read: Samsung I7500)
int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
EGLConfig[] configs = new EGLConfig[1];
int[] result = new int[1];
egl.eglChooseConfig(display, attributes, configs, 1, result);
return configs[0];
}
});

renderer = new MyRenderer();
mGLView.setRenderer(renderer);
setContentView(mGLView);
}

@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}

@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}

@Override
protected void onStop() {
super.onStop();
}

private void copy(Object src) {
try {
Logger.log("Copying data from master Activity!");
Field[] fs = src.getClass().getDeclaredFields();
for (Field f : fs) {
f.setAccessible(true);
f.set(this, f.get(src));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}



protected boolean isFullscreenOpaque() {
return true;
}


public boolean onKeyDown(int keyCode, KeyEvent event) {
SimpleVector sv;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
sv=new SimpleVector(0,1,0);
cb.setRotationPivot(sv);
cb.rotateZ((float)Math.PI/10);
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
sv=new SimpleVector(0,1,1);
cb.setRotationPivot(sv);
cb.rotateX((float)Math.PI/10);
return true;

}
return false;
}


class MyRenderer implements GLSurfaceView.Renderer {

public MyRenderer() {
}

public Object3D createBox() {

Object3D cube=new Object3D(12);

cube.addTriangle( new SimpleVector( 0, 0, 0 ),
0, 0,
new SimpleVector( 0, 1, 0 ), 0, 1,
new SimpleVector( 1, 1, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 1, 0 ),
1, 1,
new SimpleVector( 1, 0, 0 ), 1, 0,
new SimpleVector( 0, 0, 0 ), 0, 0);

cube.addTriangle( new SimpleVector( 0, 0, 1 ),
0, 0,
new SimpleVector( 0, 1, 1 ), 0, 1,
new SimpleVector( 0, 1, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 0, 1, 0 ),
1, 1,
new SimpleVector( 0, 0, 0 ), 1, 0,
new SimpleVector( 0, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 1, 0, 0 ),
0, 0,
new SimpleVector( 1, 1, 0 ), 0, 1,
new SimpleVector( 1, 1, 1 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 1, 1 ),
1, 1,
new SimpleVector( 1, 0, 1 ), 1, 0,
new SimpleVector( 1, 0, 0 ), 0, 0);
cube.addTriangle( new SimpleVector( 1, 0, 1 ),
0, 0,
new SimpleVector( 1, 1, 1 ), 0, 1,
new SimpleVector( 0, 1, 1 ), 1, 1);
cube.addTriangle( new SimpleVector( 0, 1, 1 ),
1, 1,
new SimpleVector( 0, 0, 1 ), 1, 0,
new SimpleVector( 1, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 0, 0, 1 ),
0, 0,
new SimpleVector( 0, 0, 0 ), 0, 1,
new SimpleVector( 1, 0, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 0, 0 ),
1, 1,
new SimpleVector( 1, 0, 1 ), 1, 0,
new SimpleVector( 0, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 0, 1, 0 ),
0, 0,
new SimpleVector( 0, 1, 1 ), 0, 1,
new SimpleVector( 1, 1, 1 ), 1, 1 );
cube.addTriangle( new SimpleVector( 1, 1, 1 ),
1, 1,
new SimpleVector( 1, 1, 0 ), 1, 0,
new SimpleVector( 0, 1, 0 ), 0, 0 );

cube.setCenter(new SimpleVector(0,0,0));

return cube;
}



public void buildObj() {
if (master == null) {
world = new World();
cb=createBox();
cb.translate(-1, 0, -1);
cb.setAdditionalColor(c1);
cb.build();
world.addObject(cb);
cb=createBox();
cb.translate(-1, -1, -1);
cb.setAdditionalColor(c2);
cb.build();
world.addObject(cb);
world.getCamera().setPosition(-0.5f, -0.5f, -5);
world.getCamera().lookAt(new SimpleVector(-0.5,-0.5,-0.5));


}
}


public void onSurfaceChanged(GL10 gl, int w, int h) {
if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(gl, w, h);
buildObj();
world.buildAllObjects();

}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

public void onDrawFrame(GL10 gl) {

fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();
}
}
}
#11
Support / Re: rotate around edge
April 04, 2012, 03:12:24 AM
I have tried to understand how rotation of a cube  around a pivot works to no avail. What I am trying to achieve is to be able to rotate reliably a cube around any one of its edge. Here is what I am doing:

  • create a cube with corners (0,0,0) and (1,1,1)
  • translate the cube to (-1,-1,-1)
  • set the pivot to (0,1,0) and rotate the the cube around the Z axis Math.PI radians. The cube rotates left as expected (relative to where the camera is set)
  • set the pivot to (0,1,1) and rotate the cube around the X axis Math.PI radians)
I was expecting to have the cube to rotate around the lower back edge but it rotated around the top back edge.
I tried several combinations of pivot setting but nothing worked.

I have looked at a couple of examples and I thought I had understood how rotation around a pivot worked but I figured I was wrong...

Looking for some help