collision example how helloword-ae

Started by ggp, February 08, 2014, 06:55:03 PM

Previous topic - Next topic

ggp

Long time ago i not visited this site...

i search a simple example collision proyect android jpct-ae

-sphere down in plane (gravity) for example

-or move sphere and colide with cube for example

I am not able to implement, i read wiki

http://www.jpct.net/wiki/index.php/Collision_detection

I do not understand

ggp

this is perfect example, I've finally tested in jpct

http://www.jpct.net/wiki/index.php/Collision_detection

but in jpct-ae the code implementation is different




EgonOlsen

It's actually the same thing in jPCT-AE except for, of course, the framework around it. Instead of a main method in some class, you would create an Activity and instead of evaluating keyboard input you would have to do something else. But the actual collision detection code is exactly the same. Does that help somehow?

ggp

I got it :D

but can be improved?


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.KeyEvent;
import android.view.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
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.World;
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(255, 0, 0);

private float touchTurn = 0;
private float touchTurnUp = 0;

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


//private int fps = 0;

private Light sun = null;

//------------------------
private Texture font = null;

private float move = 0;

private static final long serialVersionUID = 1L;

private static final float DAMPING = 0.1f;

private static final float SPEED = 1f;

private static final float MAXSPEED = 1f;

private Object3D plane = null;

private Object3D ramp = null;

private Object3D cube = null;

private Object3D cube2 = null;

private Object3D sphere = null;



private SimpleVector moveRes = new SimpleVector(0, 0, 0);

private SimpleVector ellipsoid = new SimpleVector(2, 2, 2);


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

public boolean onTouchEvent(MotionEvent me) {

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


public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
move = 1f;
return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
move = -1f;
return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
cube.rotateY((float) Math.toRadians(-1));
return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
cube.rotateY((float) Math.toRadians(1));
return true;
}



return super.onKeyDown(keyCode, msg);
}

public boolean onKeyUp(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
move = 0;
return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
move = 0;
return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

return true;
}

if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {

return true;
}



return super.onKeyUp(keyCode, msg);
}


protected boolean isFullscreenOpaque() {
return true;
}

class MyRenderer implements GLSurfaceView.Renderer {
private int fps = 0;
private int lfps = 0;

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

Config.maxPolysVisible = 500;
         Config.farPlane = 1500;
         Config.glTransparencyMul = 0.1f;
         Config.glTransparencyOffset = 0.1f;
         Config.useVBO=true;
         
         Texture.defaultToMipmapping(false);
         Texture.defaultTo4bpp(true);
}
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);
               
//keyMapper = new KeyMapper(this);

plane = Primitives.getPlane(20, 10);
plane.rotateX((float) Math.PI / 2f);

ramp = Primitives.getCube(20);
ramp.rotateX((float) Math.PI / 2f);

sphere = Primitives.getSphere(30);
sphere.translate(-50, 10, 50);

cube2 = Primitives.getCube(20);
cube2.translate(60, -20, 60);

cube = Primitives.getCube(2);
cube.translate(-50, -10, -50);

plane.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
ramp.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
sphere.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
cube2.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
cube.setCollisionMode(Object3D.COLLISION_CHECK_SELF);

world.addObject(plane);
world.addObject(ramp);
world.addObject(cube);
world.addObject(sphere);
world.addObject(cube2);
               
Light light = new Light(world);
light.setPosition(new SimpleVector(0, -80, 0));
light.setIntensity(140, 120, 120);
light.setAttenuation(-1);

world.setAmbientLight(20, 20, 20);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);
cam.moveCamera(Camera.CAMERA_MOVEUP, 100);
cam.lookAt(ramp.getTransformedCenter());

//long start = System.currentTimeMillis();
//long fps = 0;

SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 50;
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) {
//move();

Camera cam = world.getCamera();
cam.setPositionToCenter(cube);
cam.align(cube);
cam.rotateCameraX((float) Math.toRadians(30));
cam.moveCamera(Camera.CAMERA_MOVEOUT, 40);
         
SimpleVector t = cube.getZAxis();
t.scalarMul(move);
moveRes.add(t);

// avoid high speeds
if (moveRes.length() > MAXSPEED) {
moveRes.makeEqualLength(new SimpleVector(0, 0, MAXSPEED));
}

cube.translate(0, -0.02f, 0);

moveRes = cube.checkForCollisionEllipsoid(moveRes, ellipsoid, 8);
cube.translate(moveRes);

// finally apply the gravity:
//SimpleVector t = cube.getZAxis();
SimpleVector tt = new SimpleVector(0, 1, 0);
tt = cube.checkForCollisionEllipsoid(tt, ellipsoid, 1);
cube.translate(tt);

// damping
if (moveRes.length() > DAMPING) {
moveRes.makeEqualLength(new SimpleVector(0, 0, DAMPING));
} else {
moveRes = new SimpleVector(0, 0, 0);
}



fb.clear(back);
world.renderScene(fb);
world.draw(fb);
blitNumber(lfps, 5, 5);

fb.display();


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

private void blitNumber(int number, int x, int y) {
if (font != null) {
String sNum = Integer.toString(number);

for (int i = 0; i < sNum.length(); i++) {
char cNum = sNum.charAt(i);
int iNum = cNum - 48;
fb.blit(font, iNum * 5, 0, x, y, 5, 9, FrameBuffer.TRANSPARENT_BLITTING);
x += 5;
}
}
}
}
}

EgonOlsen

Are you having any problems with it that needs improving?