159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.bullet.objects.infos;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.objects.PhysicsVehicle;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Matrix3f;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Quaternion;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * stores transform info of a PhysicsNode in a threadsafe manner to
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * allow multithreaded access from the jme scenegraph and the bullet physicsspace
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normenhansen
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class RigidBodyMotionState {
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    long motionStateId = 0;
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Vector3f worldLocation = new Vector3f();
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Matrix3f worldRotation = new Matrix3f();
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Quaternion worldRotationQuat = new Quaternion();
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Quaternion tmp_inverseWorldRotation = new Quaternion();
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PhysicsVehicle vehicle;
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private boolean applyPhysicsLocal = false;
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//    protected LinkedList<PhysicsMotionStateListener> listeners = new LinkedList<PhysicsMotionStateListener>();
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public RigidBodyMotionState() {
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.motionStateId = createMotionState();
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created MotionState {0}", Long.toHexString(motionStateId));
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private native long createMotionState();
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * applies the current transform to the given jme Node if the location has been updated on the physics side
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param spatial
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public synchronized boolean applyTransform(Spatial spatial) {
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f localLocation = spatial.getLocalTranslation();
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quaternion localRotationQuat = spatial.getLocalRotation();
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        boolean physicsLocationDirty = applyTransform(motionStateId, localLocation, localRotationQuat);
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (!physicsLocationDirty) {
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return false;
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (!applyPhysicsLocal && spatial.getParent() != null) {
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            localLocation.divideLocal(spatial.getParent().getWorldScale());
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//            localRotationQuat.set(worldRotationQuat);
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            tmp_inverseWorldRotation.mult(localRotationQuat, localRotationQuat);
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            spatial.setLocalTranslation(localLocation);
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            spatial.setLocalRotation(localRotationQuat);
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            spatial.setLocalTranslation(localLocation);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            spatial.setLocalRotation(localRotationQuat);
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//            spatial.setLocalTranslation(worldLocation);
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//            spatial.setLocalRotation(worldRotationQuat);
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (vehicle != null) {
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            vehicle.updateWheels();
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return true;
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private synchronized native boolean applyTransform(long stateId, Vector3f location, Quaternion rotation);
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the worldLocation
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Vector3f getWorldLocation() {
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getWorldLocation(motionStateId, worldLocation);
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return worldLocation;
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private native void getWorldLocation(long stateId, Vector3f vec);
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the worldRotation
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Matrix3f getWorldRotation() {
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getWorldRotation(motionStateId, worldRotation);
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return worldRotation;
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private native void getWorldRotation(long stateId, Matrix3f vec);
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the worldRotationQuat
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Quaternion getWorldRotationQuat() {
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getWorldRotationQuat(motionStateId, worldRotationQuat);
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return worldRotationQuat;
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private native void getWorldRotationQuat(long stateId, Quaternion vec);
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param vehicle the vehicle to set
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setVehicle(PhysicsVehicle vehicle) {
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.vehicle = vehicle;
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public boolean isApplyPhysicsLocal() {
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return applyPhysicsLocal;
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setApplyPhysicsLocal(boolean applyPhysicsLocal) {
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.applyPhysicsLocal = applyPhysicsLocal;
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public long getObjectId(){
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return motionStateId;
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//    public void addMotionStateListener(PhysicsMotionStateListener listener){
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        listeners.add(listener);
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//    }
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//    public void removeMotionStateListener(PhysicsMotionStateListener listener){
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        listeners.remove(listener);
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//    }
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected void finalize() throws Throwable {
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.finalize();
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing MotionState {0}", Long.toHexString(motionStateId));
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        finalizeNative(motionStateId);
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private native void finalizeNative(long objectId);
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
164