www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: sentsent on August 04, 2016, 03:58:20 pm

Title: Rotate problem
Post by: sentsent on August 04, 2016, 03:58:20 pm
Code: [Select]
try {

            playerDummyObj = Object3D.createDummyObj();

            AssetManager.AssetInputStream fis = (AssetManager.AssetInputStream) MainActivity.MA.getResources().openRawResource(
                    R.raw.terra);
            playerObj3d = Loader.loadASC(fis, SCALE*0.20F, true);
            playerObj3d.setTexture("cliff");

            AssetManager.AssetInputStream fisShield = (AssetManager.AssetInputStream) MainActivity.MA.getResources().openRawResource(
                    R.raw.terra);
            shieldBall = Loader.loadASC(fisShield, SCALE*0.20F, true);
            shieldBall.setTexture("cliff");


        } catch (Exception e) {
            e.printStackTrace();
        }

        MainActivity.WORLD.addObject(playerObj3d);
        MainActivity.WORLD.addObject(shieldBall);

        Transform shapeTransform = new Transform();
        shapeTransform.setIdentity();
        shapeTransform.origin.set(new Vector3f(0F, 0.5F, 0F));

        Transform shieldBallTransform = new Transform();
        shieldBallTransform.setIdentity();
        shieldBallTransform.origin.set(new Vector3f(0.5F, 0.5F, 0F));

        SphereShape shape = new SphereShape(1F);
        shape.setLocalScaling(new Vector3f(SCALE,SCALE,SCALE));

        SphereShape shieldBallShape = new SphereShape(1F);
        shieldBallShape.setLocalScaling(new Vector3f(SCALE,SCALE,SCALE));

        Vector3f localInertia = new Vector3f(0, 0, 0);
        Vector3f shieldLocalInertia = new Vector3f(0, 0, 0);

        shape.calculateLocalInertia(mass, localInertia);
        shieldBallShape.calculateLocalInertia(mass, shieldLocalInertia);

        JPCTBulletMotionState ms = new JPCTBulletMotionState(playerObj3d, shapeTransform);
        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, ms, shape, localInertia);
        body = new RigidBody(rbInfo);
        Jbullet.dynamicWorld.addRigidBody(body);

        JPCTBulletMotionState msShield = new JPCTBulletMotionState(shieldBall, shieldBallTransform);
        RigidBodyConstructionInfo rbInfoShield = new RigidBodyConstructionInfo(mass, msShield, shieldBallShape, shieldLocalInertia);
        RigidBody shieldBody = new RigidBody(rbInfoShield);
        Jbullet.dynamicWorld.addRigidBody(shieldBody);

        playerDummyObj.translate(0F, 2F, 0F);
        shieldBall.addParent(playerDummyObj);

        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {

//                   playerDummyObj.clearTranslation();
                    playerDummyObj.rotateAxis(playerDummyObj.getRotationMatrix().getYAxis(),-0.02f);

                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

Hello, I have such a code and my problem is to get  shieldBall run around the invisible playerDummyObj with Y axis.

What I observe is:  nothing happens in a few seconds, then rapidly shieldBall rotates around itself and goes away in three directions at the same time very fast (it's like a photon :D )

Any idea how to make my shieldBall slowly run around playerDummyObj  like an earth around the sun?

Title: Re: Rotate problem
Post by: EgonOlsen on August 04, 2016, 09:48:34 pm
The first thing to notice here, is that you are doing the rotation in a thread. Don't do that...just don't. jPCT-AE isn't thread safe. Doing the rotation in a thread in parallel to the rendering thread is asking for all kinds of problems. Just move the rotation code into the render thread and make the rotation value time based. If nothing else is wrong, it should work much better then.
Title: Re: Rotate problem
Post by: sentsent on August 05, 2016, 12:49:43 pm
Thank You for advice.

Now I understand " thread safe" rules. I have something wrong in code. I have moved "thread code" to onDrawFrame in GLSurfaceView.Renderer and unfortunatelly effect is the same.

Anyway I changed my mind and I dont' want to create and rotate dummy object, because i figured out that I can't add rigidbody as a child to dummy object.
So I'll try my rigidbody go forward with setLinearVelocity, and somehow turn left each 100ms (for example). But applyTorqueImpulse does not change rigidbody move direction.