project crash when remove Object3D from world JPCT AE

Started by viniciusDSL, October 08, 2012, 05:18:19 PM

Previous topic - Next topic

viniciusDSL

Hello, I start with this engine JPCT AE and I try to make a game similar to guitar hero, but i have a problem when try to remove Object3D from world, this is my code:


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

if (master == null) {


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

cube = Primitives.getCube(2);
cube.calcTextureWrapSpherical();
cube.setTexture("texture");


cube1 = Primitives.getCube(2);
cube1.calcTextureWrapSpherical();
cube1.setTexture("texture");


cube2 = Primitives.getCube(2);
cube2.calcTextureWrapSpherical();
cube2.setTexture("texture");

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(new SimpleVector(0, 0, 0));

MemoryHelper.compact();

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

public void onDrawFrame(GL10 gl) {

if (h.mTicksElapsed + 500 >= midiFile.getTick()) {
Object3D clon1 = null;
switch ((((NoteOn) midiFile).getNoteValue() % 12) % 3) {
case 0:
                                        //Create clon and add to world and LinkedList
clon1 = cube.cloneObject();
clon1.build();
clon1.setOrigin(new SimpleVector(0, -30, 10));
world.addObject(clon1);
clon1.rotateX(-10);
notes.add(clon1);
break;

case 1:
                                        //Create clon and add to world and LinkedList
clon1 = cube1.cloneObject();
clon1.build();
clon1.setOrigin(new SimpleVector(0, -30, 10));
world.addObject(clon1);
clon1.rotateX(-10);
notes.add(clon1);
break;

case 2:
                                        //Create clon and add to world and LinkedList
clon1 = cube2.cloneObject();
clon1.build();
clon1.setOrigin(new SimpleVector(0, -30, 10));
world.addObject(clon1);
clon1.rotateX(-10);
notes.add(clon1);
break;
}
midiFile = midi.nextOn();
}
fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();
notes2.addAll(notes);
Iterator<Object3D> i = notes2.iterator();
Object3D _it;
while (i.hasNext()) {
_it = i.next();
_it.translate(0, 0.4f, -0.3f);
if (_it.getCenter().y > -10f) {
removeNote(_it, i);
                                        //i try to remove when the object is not visible in the smartphone
world.removeObject(_it);
}
}
notes2.addAll(notes);
notes.clear();
}
}
 




when run i have this:

10-08 11:17:53.557: E/AndroidRuntime(3188): FATAL EXCEPTION: main
10-08 11:17:53.557: E/AndroidRuntime(3188): java.lang.RuntimeException: [ 1349709473570 ] - ERROR: Can't remove object #3, because it doesn't belong to this World!
10-08 11:17:53.557: E/AndroidRuntime(3188):    at com.threed.jpct.Logger.log(Logger.java:189)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at com.threed.jpct.World.removeObject(World.java:267)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at com.binizzio.brokenstringsv1.BrokenStrings$3.run(BrokenStrings.java:402)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at android.os.Handler.handleCallback(Handler.java:587)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at android.os.Looper.loop(Looper.java:130)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at android.app.ActivityThread.main(ActivityThread.java:3835)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at java.lang.reflect.Method.invokeNative(Native Method)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at java.lang.reflect.Method.invoke(Method.java:507)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
10-08 11:17:53.557: E/AndroidRuntime(3188):    at dalvik.system.NativeStart.main(Native Method)

Thanks for the help and best regards.
PD: sorry my english is bad

EgonOlsen

The exception says

QuoteCan't remove object #3, because it doesn't belong to this World!

...so you are removing something from a world that isn't a part of it. Just stop doing that... ;) Apart from that, make sure that you removing and adding objects either at startup only or in the rendering thread. Don't do it in listener callbacks from touch or other events.

viniciusDSL

Hello, thanks for reply, now know little of this engine, i want remove Object3D when this is not visible in the smartphone, but do not know the methods that run each second  :-\ , where I can find them?

PD:sorry for my english

EgonOlsen

I don't get the question...which "method that run each second" do you mean?

viniciusDSL

sorry, method that is running all the time in the application, how onDrawFrame()


EgonOlsen

There is none. Just onDrawFrame(). The rest is triggered by Android via events and such.

viniciusDSL

Ok, I understand, very thanks for the help   ;D  , now you recommend me alternative to "world.remove (myObject);" to liberate the space occupied of the Object in the memory?, because when I add objects, the world is very slow.

EgonOlsen

It's ok to call remove() IF the object has actually been added before. But you are removing objects that haven't been added, which causes the problem. I don't see how and unadded object can slow things down. To free the memory of an unadded object, just null the instance. But then again, i don't think that i really got your question... ???

viniciusDSL

this is a video of mi project, in this you can see my problem, while the time passed the world have lag...the objects lose velocity...

http://minus.com/lgMkgpnDGhIta

sorry for insisting on the topic, but I love this project and I want to finish it  ;D, and thanks for help

dutch_delight

Not sure what youre doing but if your object is off screen, you remove it. Then the next frame its off screen again and you remove it again resulting in the object being removed when its already removed.

Also, all this adding and removing will result in a lot of garbage collecting and will affect the frame rate of your game. What I do with my new game is have 10 objects in an array. When I don't need them I hide them and when I do need them I make them visible and reset their position so they're ready for processing again.

Also, don't define an object3d  In the render loop. That will add to the garbage collecting. Define a temporary 3dobject when you initialize your app. That way it will always be available to reuse without it needing to be recycled.

Why are you adding notes to notes2 twice?

EgonOlsen

Yes, try to handle visiblity with setVisibility of pooled objects instead. By constantly adding and removing objects, you are doing the engine's work in your own code.

viniciusDSL

very thanks for reply, it's idea works ^_^

PD: sorry my english is bad.