www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Thomas. on August 05, 2011, 07:25:03 pm

Title: dummy
Post by: Thomas. on August 05, 2011, 07:25:03 pm
Hi Egon, I have problem with dummy. I need add dummy to another dummy, but when I do this, object (from code) is not visible. Any idea?

Code: [Select]
for (int i = 0; i < moveMergeIds.size(); i += 2) { // sjednoceni pohybu definovanych objektu (f -> s)
int idf = moveMergeIds.get(i);
int ids = moveMergeIds.get(i + 1);
Object3D object = objectManager.getObject(idf).getDummy();
Object3D dummy = objectManager.getObject(ids).getDummy();
dummy.addChild(object);
}
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 08:32:54 pm
dummy -> dummy...object from code...i don't get it...what's the actual problem?
Title: Re: dummy
Post by: Thomas. on August 05, 2011, 08:42:33 pm
all children of dummy (in code it is variable "object") are not visible, when I use this part of code
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 09:11:53 pm
So...you have (non-dummy) objects that are children of dummy and if you add another dummy as child of dummy, those objects suddenly disappear?
Title: Re: dummy
Post by: Thomas. on August 05, 2011, 09:24:41 pm
both Object3D are dummy objects created by static method createDummyObj()...
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 09:26:01 pm
Yes, i know...but that doesn't answer my question...
Title: Re: dummy
Post by: Thomas. on August 05, 2011, 09:30:03 pm
answer is exactly :)
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 09:34:17 pm
Maybe you are getting an error because Config.maxParentObjects is exceeded?
Title: Re: dummy
Post by: Thomas. on August 05, 2011, 09:44:28 pm
I set a higher number, but it is still same
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 09:46:40 pm
Makes no sense to me. Test case?
Title: Re: dummy
Post by: Thomas. on August 05, 2011, 10:36:14 pm
Now I have found that objects are on another positions that I want.

Code: [Select]
package cz.test.point_light;

import java.lang.reflect.Field;

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

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.GLSLShader;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
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.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.AAConfigChooser;
import com.threed.jpct.util.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;

/**
 * @author EgonOlsen
 *
 */
public class Point_light_testActivity extends Activity {

private static Point_light_testActivity 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 Object3D cube0 = null;
private Object3D cube1 = null;
private Object3D cube2 = null;
private Object3D cube3 = null;
private Object3D dummy = null;

private int fps = 0;

private Light light;

protected void onCreate(Bundle savedInstanceState) {

Logger.log("onCreate");

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

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

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

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

mGLView.setEGLContextClientVersion(2);
mGLView.setEGLConfigChooser(new AAConfigChooser(mGLView));

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

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

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(w, h);

if (master == null) {

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

Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64,
64));
TextureManager.getInstance().addTexture("texture", texture);

dummy = Object3D.createDummyObj();

cube0 = Primitives.getCube(10);
cube0.rotateY(-(float) Math.PI / 4f);
cube0.rotateMesh();
cube0.clearRotation();
cube0.calcTextureWrapSpherical();
cube0.setTexture("texture");
cube0.strip();
cube0.build();

cube1 = cube0.cloneObject();
cube2 = cube0.cloneObject();
cube3 = cube0.cloneObject();

world.addObject(cube0);
world.addObject(cube1);
/*world.addObject(cube2);
world.addObject(cube3);*/

/*cube0.translate(-20, -20, 0);
cube1.translate(20, -20, 0);
cube2.translate(-20, 20, 0);
cube3.translate(20, 20, 0);*/

Object3D dummy2 = Object3D.createDummyObj();
cube0.addParent(dummy2);
dummy2.translate(-30, 0, 0);
cube1.addParent(dummy);
dummy.translate(30, 0, 0);
dummy.addChild(dummy2);

// is it same?
//cube0.translate(-30, 0, 0);
//cube1.translate(30, 0, 0);

Object3D plane = Primitives.getPlane(40, 3);
plane.translate(0, 0, 10);
plane.build();
world.addObject(plane);

Resources res = getResources();

light = new Light(world);
light.setPosition(new SimpleVector(20, 0, -20));

light.setIntensity(255, 255, 255);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);

MemoryHelper.compact();

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

// Logger.setLogLevel(Logger.DEBUG);
}
}

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

public void onDrawFrame(GL10 gl) {

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

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

light.rotate(new SimpleVector(0, 0, 0.01), new SimpleVector());

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);
}
}
}
}
Title: Re: dummy
Post by: EgonOlsen on August 05, 2011, 11:07:32 pm
Thanks for the test case. Behaviour is perfectly fine. Maybe it's clearer if you change

Code: [Select]
dummy.addChild(dummy2);
to

Code: [Select]
dummy2.addParent(dummy);
which is the same thing, just with reversed thinking. So dummy becomes a parent of dummy2, meaning that any object that has dummy2 as parent also inherits the transformations of dummy, i.e. -30+30=0
Title: Re: dummy
Post by: Thomas. on August 06, 2011, 11:46:44 am
without any changes...
Title: Re: dummy
Post by: EgonOlsen on August 06, 2011, 12:16:57 pm
No idea what you mean... ???
Title: Re: dummy
Post by: Thomas. on August 06, 2011, 12:39:44 pm
I do not know why object has bad position... first object is translated by first dummy, second object by second dummy and set that second will also translate with first in future...
Title: Re: dummy
Post by: EgonOlsen on August 06, 2011, 12:41:44 pm
But i've already explained that. You are making dummy a parent of dummy2, which nulls the translation for everything that is a child of dummy2.
Title: Re: dummy
Post by: Thomas. on August 06, 2011, 12:54:32 pm
I do not know what about you talking :) I just add child and does not any translation after dummy.addChild(dummy2); ... so when I add new child every previous translation and rotation of dummy are applied on child?
Title: Re: dummy
Post by: EgonOlsen on August 06, 2011, 12:58:17 pm
cube0 is a child of dummy2, inheriting it's transformations. dummy2 becomes a child of dummy, inheriting it's transformations. So the transformation is dummy->dummy2->cube0. And because dummy and dummy2 result in no transformation, you get the current results.
Title: Re: dummy
Post by: Thomas. on August 06, 2011, 01:05:35 pm
oh now I understand, thanks for patience :)
Title: Re: dummy
Post by: Thomas. on August 06, 2011, 03:46:30 pm
Is possible do it if I have these two dummies? (with "correct" position and rotation)
Title: Re: dummy
Post by: EgonOlsen on August 06, 2011, 09:38:32 pm
Sure. If the second child compensates for the translation of the first one. But i'm not sure if this is the right solution to your problem...which i don't know, so it might very well be.
Title: Re: dummy
Post by: Thomas. on August 07, 2011, 10:53:51 am
All objects and their properties are loaded from xml. Every object (ex. car) can has more objects (for shoot, move, interaction and object visible on screen), these objects are in one array and every array has dummy. So I load elevator and control panel, that has in properties united movement with the elevator.
Title: Re: dummy
Post by: EgonOlsen on August 08, 2011, 07:53:28 pm
For your example, i'm not sure why you need multiple dummies then...anyway, you just have to make sure that your transformation chain gives you the results you want when chaining multiple transformations. You can create the wildest chains of transformations...it's totally up to you.