Author Topic: CollisionEvent never called  (Read 431 times)

Offline y_muse

  • byte
  • *
  • Posts: 4
    • View Profile
CollisionEvent never called
« on: December 16, 2015, 12:06:22 am »
Hi! I'm trying to detect a CollisionEvent but I just can't get it right.

I made a very simple project with two cubes colliding...the problem is that neither of the two CollisionEvents is called, am I missing something in the initialization of the Objects? Following is the test project:

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 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) {
cube1.translate(0.1f, 0f, 0f);

time = System.currentTimeMillis();
}

} else {
if (buffer != null) {
buffer.dispose();
buffer = null;
}
}
} catch (Exception e) {
Logger.log(e, Logger.MESSAGE);
}
}

}
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: CollisionEvent never called
« Reply #1 on: December 16, 2015, 08:51:27 am »
You have to detect the collision actively. For example by using this method: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#checkForCollisionEllipsoid(com.threed.jpct.SimpleVector, com.threed.jpct.SimpleVector, int)

A simple translate() just translates, but it doesn't check for collision by itself. The idea is to feed the actual translation vector into one of the collision methods and then use the resulting vector as the actual translation.

More info: http://www.jpct.net/wiki/index.php?title=Collision_detection

Offline y_muse

  • byte
  • *
  • Posts: 4
    • View Profile
Re: CollisionEvent never called
« Reply #2 on: December 16, 2015, 06:07:49 pm »
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:
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);
}
}

}
}