Great! thank you, I had already checked that example but I didn't understand that it was the CheckForCollisionEllipsoid function throwing the CollisionEvent.
For reference this is the correct solution of my example:
For reference this is the correct solution of my example:
Code Select
package com.threed.jpct.example;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import com.threed.jpct.*;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class HelloWorld extends Activity {
private static HelloWorld master = null;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer buffer = null;
private World world = null;
private RGBColor back = new RGBColor(50, 50, 100);
private SimpleVector moveCube = new SimpleVector(0, 0, 0), ellipsoid = new SimpleVector(1, 1, 1);
private Object3D cube1 = null;
private Object3D cube2 = null;
private Light sun = null;
private Camera cam = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
renderer = new MyRenderer();
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();
}
class MyRenderer implements GLSurfaceView.Renderer {
private long time = System.currentTimeMillis();
private boolean stop = false;
public MyRenderer() {
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
if (buffer != null) {
buffer.dispose();
}
buffer = new FrameBuffer(gl, w, h);
if (master == null) {
world = new World();
world.setAmbientLight(150, 150, 150);
sun = new Light(world);
sun.setIntensity(250, 250, 250);
cube1 = Primitives.getCube(1);
cube2 = Primitives.getCube(1);
cube1.setCollisionMode(Object3D.COLLISION_CHECK_SELF | Object3D.COLLISION_CHECK_OTHERS);
cube1.setCollisionOptimization(true);
cube1.addCollisionListener(new CollisionListener() {
@Override
public void collision(CollisionEvent collisionEvent) {
Logger.log("Collision1");
}
@Override
public boolean requiresPolygonIDs() {
return false;
}
});
cube2.translate(15f, 0f, 0f);
cube2.setCollisionMode(Object3D.COLLISION_CHECK_SELF | Object3D.COLLISION_CHECK_OTHERS);
cube2.setCollisionOptimization(true);
cube2.addCollisionListener(new CollisionListener() {
@Override
public void collision(CollisionEvent collisionEvent) {
Logger.log("Collision2");
}
@Override
public boolean requiresPolygonIDs() {
return false;
}
});
cube1.build();
cube2.build();
world.addObject(cube1);
world.addObject(cube2);
cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.moveCamera(Camera.CAMERA_MOVEUP, 15);
SimpleVector cameraView = cube1.getTransformedCenter();
cameraView.x += 10;
cam.lookAt(cameraView);
cam.setFOV(cam.getMinFOV());
SimpleVector sv = new SimpleVector();
sv.set(cube1.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
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) {
buffer.clear(back);
world.renderScene(buffer);
world.draw(buffer);
buffer.display();
if (System.currentTimeMillis() - time >= 10) {
moveCube.set(0.1f, 0f, 0f);
moveCube = cube1.checkForCollisionEllipsoid(moveCube, ellipsoid, 8);
cube1.translate(moveCube);
time = System.currentTimeMillis();
}
} else {
if (buffer != null) {
buffer.dispose();
buffer = null;
}
}
} catch (Exception e) {
Logger.log(e, Logger.MESSAGE);
}
}
}
}