www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Nemetz on July 06, 2011, 03:15:19 pm

Title: Picking question
Post by: Nemetz on July 06, 2011, 03:15:19 pm
Hello!
As i saw in wiki article about picking of object there are 2 ways, and for AE as i understanding what second way is more implementabe, bacause in the firts you i must use Interact2D.pickPolygon() method which excluded from AE version of JPCT.
Give me advice, please, how can i pick the polygon in AE.
Thank you.
Title: Re: Picking question
Post by: EgonOlsen on July 06, 2011, 09:40:39 pm
Yes, the first way doesn't work on AE. Just use the second way.
Title: Re: Picking question
Post by: Nemetz on July 08, 2011, 12:08:53 pm
Second way works great and fast, but i need pick a polygon.
Maybe normals of polyons will help me? Or there is another way in AE?
Title: Re: Picking question
Post by: EgonOlsen on July 08, 2011, 02:03:42 pm
Just implement a collision listener and let that implementation return true in requiresPolygonIDs() to get the affected ids in the collision event.
Title: Re: Picking question
Post by: Nemetz on July 29, 2011, 10:55:34 am
Thank you!
I implemented CollisionListener into my objects, and overrided 2 methods.
Picking object like  this:
Code: [Select]
SimpleVector dir = Interact2D.reproject2D3DWS(cam, fb, x, y)
.normalize();
Object[] res = world.calcMinDistanceAndObject3D(cam.getPosition(), dir,
distProjection);
but method
Code: [Select]
public void collision(CollisionEvent ce) {
doesnt' calling...
Title: Re: Picking question
Post by: EgonOlsen on July 29, 2011, 05:48:53 pm
Have you set the collision mode correctly? (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#setCollisionMode(int) (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#setCollisionMode(int)))
Title: Re: Picking question
Post by: Nemetz on August 01, 2011, 10:08:16 am
Of course, after object initiation set
   object.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
Title: Re: Picking question
Post by: EgonOlsen on August 01, 2011, 10:31:00 am
What's the return value of the method call? Consider also to adjust http://www.jpct.net/doc/com/threed/jpct/Config.html#collideOffset (http://www.jpct.net/doc/com/threed/jpct/Config.html#collideOffset).
Title: Re: Picking question
Post by: Nemetz on August 01, 2011, 11:02:30 am
Already increase this value in Config...
Maybe i'm doing something wrong?
Title: Re: Picking question
Post by: EgonOlsen on August 01, 2011, 11:33:21 am
Obviously... ;) Do you have a simple test case that shows the problem?
Title: Re: Picking question
Post by: Nemetz on August 02, 2011, 10:30:27 am
Here is a project exmaple, so, in logcat you can see, what project is picking, but the method public void collision(CollisionEvent ce)
doesnt call, as i understand, that method just like callback...Maybe mistake in this point.


Code: [Select]
package com.threed.jpct.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.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
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;

/**
 * A simple demo. This shows more how to use jPCT-AE than it shows how to write
 * a proper application for Android.
 * It includes basic activity management to handle pause and resume...
 *
 * @author EgonOlsen
 *
 */
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 float touchTurn = 0;
private float touchTurnUp = 0;

private float xpos = -1;
private float ypos = -1;

private TextureBox cube = null;
private int fps = 0;

private Light sun = null;
Camera cam = new Camera();
TextureBox pickedObject;

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();
Config.collideOffset = 500;
mGLView.setRenderer(renderer);
setContentView(mGLView);
}

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

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

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

private TextureBox getTouchedBox(int x, int y) {


SimpleVector dir = Interact2D.reproject2D3DWS(cam, fb, x, y)
.normalize();
Object[] res = world.calcMinDistanceAndObject3D(cam.getPosition(), dir,
300);
TextureBox b = null;
if (res[1] != null && res[1] instanceof TextureBox) {
b = ((TextureBox) res[1]);
}
return b;

}

public boolean onTouchEvent(MotionEvent me) {

pickedObject = getTouchedBox((int)me.getX(), (int)me.getY());
if (pickedObject != null){
Logger.log("Object name is:" + pickedObject.getName());
}
if (me.getAction() == MotionEvent.ACTION_DOWN) {
xpos = me.getX();
ypos = me.getY();
return true;
}

if (me.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
return true;
}

if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;

xpos = me.getX();
ypos = me.getY();

touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
return true;
}

try {
Thread.sleep(15);
} catch (Exception e) {
// No need for this...
}

return super.onTouchEvent(me);
}

protected boolean isFullscreenOpaque() {
return true;
}

class MyRenderer implements GLSurfaceView.Renderer {

private long time = System.currentTimeMillis();
private boolean stop = false;

public MyRenderer() {
}

public void stop() {
stop = true;
}

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

if (master == null) {

world = new World();
world.setAmbientLight(20, 20, 20);

sun = new Light(world);
sun.setIntensity(250, 250, 250);

// Create a texture out of the icon...:-)
Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64, 64));
TextureManager.getInstance().addTexture("texture", texture);
TextureManager.getInstance().addTexture("beetle",
new Texture(getResources().openRawResource(R.drawable.beetle), true));
cube = new TextureBox("beetle", // left
"beetle",// front,
"beetle",// right,
"beetle",// back,
"beetle",// up,
"beetle",// down,
15, TextureManager.getInstance());
cube.setTransparency(50);
cube.setName("simple cube");
cube.build();
cube.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
world.addObject(cube);

cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(cube.getTransformedCenter());

SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();

if (master == null) {
Logger.log("Saving master Activity!");
master = HelloWorld.this;
}
}
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

public void onDrawFrame(GL10 gl) {

try {
if (!stop) {
if (touchTurn != 0) {
cube.rotateY(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
cube.rotateX(touchTurnUp);
touchTurnUp = 0;
}

fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();

if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
} else {
if (fb != null) {
fb.dispose();
fb = null;
}
}
} catch (Exception e) {
Logger.log(e, Logger.MESSAGE);
}
}
}
}


Code: [Select]
package com.threed.jpct.example;

import android.content.Intent;
import android.graphics.drawable.shapes.ArcShape;
import android.util.FloatMath;
import android.util.Log;

import com.threed.jpct.CollisionEvent;
import com.threed.jpct.CollisionListener;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.TextureManager;

public class TextureBox extends Object3D implements
CollisionListener {

/**
* serial version of UID
*/
private static final long serialVersionUID = -74005917658996332L;
/**
*
*/
int size = 10;
public float angle = 0;



private static SimpleVector upperLeftFront = null;
private static SimpleVector upperRightFront = null;
private static SimpleVector lowerLeftFront = null;
private static SimpleVector lowerRightFront = null;

private static SimpleVector upperLeftBack = null;
private static SimpleVector upperRightBack = null;
private static SimpleVector lowerLeftBack = null;
private static SimpleVector lowerRightBack = null;




public TextureBox(String textureName, int size) {
super(size);
this.setTexture(textureName);
}

public TextureBox(java.lang.String left, java.lang.String front,
java.lang.String right, java.lang.String back, java.lang.String up,
java.lang.String down, int size, TextureManager textureManager) {
super(size);
upperLeftFront = new SimpleVector(-size, -size, -size);
upperRightFront = new SimpleVector(size, -size, -size);
lowerLeftFront = new SimpleVector(-size, size, -size);
lowerRightFront = new SimpleVector(size, size, -size);

upperLeftBack = new SimpleVector(-size, -size, size);
upperRightBack = new SimpleVector(size, -size, size);
lowerLeftBack = new SimpleVector(-size, size, size);
lowerRightBack = new SimpleVector(size, size, size);

addTextureFront(front, textureManager, 0);
addTextureLeft(left, textureManager, 0);
addTextureBack(back, textureManager, 0);
addTextureRight(right, textureManager, 0);
addTextureUp(up, textureManager, 0);
addTextureDown(down, textureManager, 0);
this.build();
}

public TextureBox(int size, TextureManager texturemanager) {
this(null, null, null, null, null, null, size, texturemanager);
}


public void addTextureUp(String name, TextureManager textureManager, int seq) {
int textureId = isTexturePresent(name, textureManager);
try {
this.addTriangle(upperLeftBack, 0, 0, upperLeftFront, 0, 1,
upperRightBack, 1, 0, textureId, seq);
this.addTriangle(upperRightBack, 1, 0, upperLeftFront, 0, 1,
upperRightFront, 1, 1, textureId, seq);

} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture up");

}
}



public void addTextureDown(String name, TextureManager textureManager,
int seq) {
int textureId = isTexturePresent(name, textureManager);
try {
this.addTriangle(lowerLeftBack, 0, 0, lowerRightBack, 1, 0,
lowerLeftFront, 0, 1, textureId, seq);
this.addTriangle(lowerRightBack, 1, 0, lowerRightFront, 1, 1,
lowerLeftFront, 0, 1, textureId, seq);
} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture down");

}

}


public void addTextureFront(String name, TextureManager textureManager,
int seq) {

int textureId = isTexturePresent(name, textureManager);
try {
this.addTriangle(upperLeftFront, 0, 0, lowerLeftFront, 0, 1,
upperRightFront, 1, 0, textureId, seq);
this.addTriangle(upperRightFront, 1, 0, lowerLeftFront, 0, 1,
lowerRightFront, 1, 1, textureId, seq);
} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture front");

}

}

public void addTextureLeft(String name, TextureManager textureManager,
int seq) {
int textureId = isTexturePresent(name, textureManager);
try {
this.addTriangle(upperLeftFront, 1, 0, upperLeftBack, 0, 0,
lowerLeftFront, 1, 1, textureId, seq);
this.addTriangle(upperLeftBack, 0, 0, lowerLeftBack, 0, 1,
lowerLeftFront, 1, 1, textureId, seq);

} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture left");

}
}



public void addTextureBack(String name, TextureManager textureManager,
int seq) {
int textureId = isTexturePresent(name, textureManager);
try {

this.addTriangle(upperRightBack, 0, 0, lowerRightBack, 0, 1,
lowerLeftBack, 1, 1, textureId, seq);
this.addTriangle(upperLeftBack, 1, 0, upperRightBack, 0, 0,
lowerLeftBack, 1, 1, textureId, seq);

} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture back");

}

}

public void addTextureRight(String name, TextureManager textureManager,
int seq) {
int textureId = isTexturePresent(name, textureManager);
try {

this.addTriangle(upperRightFront, 0, 0, lowerRightFront, 0, 1,
upperRightBack, 1, 0, textureId, seq);
this.addTriangle(upperRightBack, 1, 0, lowerRightFront, 0, 1,
lowerRightBack, 1, 1, textureId, seq);
} catch (NullPointerException npe) {
npe.printStackTrace();
Logger.log("NPE in add Texture right");

}

}


private int isTexturePresent(String name, TextureManager textureManager) {
if (name == null || name.equals("")) {
return textureManager.getTextureID("empty");
} else {
return textureManager.getTextureID(name);
}

}


/**
*
*/
@Override
public void collision(CollisionEvent ce) {
Logger.log("COLLISION EVEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEENT");
ce.getTargets();

}

@Override
public boolean requiresPolygonIDs() {
Logger.log("requres polygon IDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
return true;
}
}
Title: Re: Picking question
Post by: EgonOlsen on August 02, 2011, 10:55:49 am
I can't find the code that actually adds the collision listener...maybe that's the problem?
Title: Re: Picking question
Post by: Nemetz on August 02, 2011, 11:02:42 am
How to add this one?
Title: Re: Picking question
Post by: EgonOlsen on August 02, 2011, 11:06:26 am
 :o http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#addCollisionListener(com.threed.jpct.CollisionListener) (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#addCollisionListener(com.threed.jpct.CollisionListener))
Title: Re: Picking question
Post by: Nemetz on August 02, 2011, 11:07:30 am
Oops, sorry(
I'm just tried to find setCollisionListener(
UPD. That helps, thank you!