Home | History | Annotate | Download | only in bullet-native
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /**
     34  * Author: Normen Hansen
     35  */
     36 
     37 #include "com_jme3_bullet_objects_PhysicsVehicle.h"
     38 #include "jmeBulletUtil.h"
     39 #include "jmePhysicsSpace.h"
     40 #include "BulletDynamics/Vehicle/btRaycastVehicle.h"
     41 
     42 #ifdef __cplusplus
     43 extern "C" {
     44 #endif
     45 
     46     /*
     47      * Class:     com_jme3_bullet_objects_PhysicsVehicle
     48      * Method:    updateWheelTransform
     49      * Signature: (JIZ)V
     50      */
     51     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_updateWheelTransform
     52     (JNIEnv *env, jobject object, jlong vehicleId, jint wheel, jboolean interpolated) {
     53         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
     54         if (vehicle == NULL) {
     55             jclass newExc = env->FindClass("java/lang/NullPointerException");
     56             env->ThrowNew(newExc, "The native object does not exist.");
     57             return;
     58         }
     59         vehicle->updateWheelTransform(wheel, interpolated);
     60     }
     61 
     62     /*
     63      * Class:     com_jme3_bullet_objects_PhysicsVehicle
     64      * Method:    createVehicleRaycaster
     65      * Signature: (JJ)J
     66      */
     67     JNIEXPORT jlong JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_createVehicleRaycaster
     68     (JNIEnv *env, jobject object, jlong bodyId, jlong spaceId) {
     69         //btRigidBody* body = reinterpret_cast<btRigidBody*> bodyId;
     70         jmeClasses::initJavaClasses(env);
     71         jmePhysicsSpace *space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
     72         if (space == NULL) {
     73             jclass newExc = env->FindClass("java/lang/NullPointerException");
     74             env->ThrowNew(newExc, "The native object does not exist.");
     75             return 0;
     76         }
     77         btDefaultVehicleRaycaster* caster = new btDefaultVehicleRaycaster(space->getDynamicsWorld());
     78         return reinterpret_cast<jlong>(caster);
     79     }
     80 
     81     /*
     82      * Class:     com_jme3_bullet_objects_PhysicsVehicle
     83      * Method:    createRaycastVehicle
     84      * Signature: (JJ)J
     85      */
     86     JNIEXPORT jlong JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_createRaycastVehicle
     87     (JNIEnv *env, jobject object, jlong objectId, jlong casterId) {
     88         jmeClasses::initJavaClasses(env);
     89         btRigidBody* body = reinterpret_cast<btRigidBody*>(objectId);
     90         if (body == NULL) {
     91             jclass newExc = env->FindClass("java/lang/NullPointerException");
     92             env->ThrowNew(newExc, "The native object does not exist.");
     93             return 0;
     94         }
     95         body->setActivationState(DISABLE_DEACTIVATION);
     96         btVehicleRaycaster* caster = reinterpret_cast<btDefaultVehicleRaycaster*>(casterId);
     97         if (caster == NULL) {
     98             jclass newExc = env->FindClass("java/lang/NullPointerException");
     99             env->ThrowNew(newExc, "The native object does not exist.");
    100             return 0;
    101         }
    102         btRaycastVehicle::btVehicleTuning tuning;
    103         btRaycastVehicle* vehicle = new btRaycastVehicle(tuning, body, caster);
    104         return reinterpret_cast<jlong>(vehicle);
    105 
    106     }
    107 
    108     /*
    109      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    110      * Method:    setCoordinateSystem
    111      * Signature: (JIII)V
    112      */
    113     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_setCoordinateSystem
    114     (JNIEnv *env, jobject object, jlong vehicleId, jint right, jint up, jint forward) {
    115         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    116         if (vehicle == NULL) {
    117             jclass newExc = env->FindClass("java/lang/NullPointerException");
    118             env->ThrowNew(newExc, "The native object does not exist.");
    119             return;
    120         }
    121         vehicle->setCoordinateSystem(right, up, forward);
    122     }
    123 
    124     /*
    125      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    126      * Method:    addWheel
    127      * Signature: (JLcom/jme3/math/Vector3f;Lcom/jme3/math/Vector3f;Lcom/jme3/math/Vector3f;FFLcom/jme3/bullet/objects/infos/VehicleTuning;Z)J
    128      */
    129     JNIEXPORT jint JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_addWheel
    130     (JNIEnv *env, jobject object, jlong vehicleId, jobject location, jobject direction, jobject axle, jfloat restLength, jfloat radius, jobject tuning, jboolean frontWheel) {
    131         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    132         if (vehicle == NULL) {
    133             jclass newExc = env->FindClass("java/lang/NullPointerException");
    134             env->ThrowNew(newExc, "The native object does not exist.");
    135             return 0;
    136         }
    137         btVector3 vec1 = btVector3();
    138         btVector3 vec2 = btVector3();
    139         btVector3 vec3 = btVector3();
    140         jmeBulletUtil::convert(env, location, &vec1);
    141         jmeBulletUtil::convert(env, direction, &vec2);
    142         jmeBulletUtil::convert(env, axle, &vec3);
    143         btRaycastVehicle::btVehicleTuning tune;
    144         btWheelInfo* info = &vehicle->addWheel(vec1, vec2, vec3, restLength, radius, tune, frontWheel);
    145         int idx = vehicle->getNumWheels();
    146         return idx-1;
    147     }
    148 
    149     /*
    150      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    151      * Method:    resetSuspension
    152      * Signature: (J)V
    153      */
    154     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_resetSuspension
    155     (JNIEnv *env, jobject object, jlong vehicleId) {
    156         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    157         if (vehicle == NULL) {
    158             jclass newExc = env->FindClass("java/lang/NullPointerException");
    159             env->ThrowNew(newExc, "The native object does not exist.");
    160             return;
    161         }
    162         vehicle->resetSuspension();
    163     }
    164 
    165     /*
    166      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    167      * Method:    applyEngineForce
    168      * Signature: (JIF)V
    169      */
    170     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_applyEngineForce
    171     (JNIEnv *env, jobject object, jlong vehicleId, jint wheel, jfloat force) {
    172         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    173         if (vehicle == NULL) {
    174             jclass newExc = env->FindClass("java/lang/NullPointerException");
    175             env->ThrowNew(newExc, "The native object does not exist.");
    176             return;
    177         }
    178         vehicle->applyEngineForce(force, wheel);
    179     }
    180 
    181     /*
    182      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    183      * Method:    steer
    184      * Signature: (JIF)V
    185      */
    186     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_steer
    187     (JNIEnv *env, jobject object, jlong vehicleId, jint wheel, jfloat value) {
    188         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    189         if (vehicle == NULL) {
    190             jclass newExc = env->FindClass("java/lang/NullPointerException");
    191             env->ThrowNew(newExc, "The native object does not exist.");
    192             return;
    193         }
    194         vehicle->setSteeringValue(value, wheel);
    195     }
    196 
    197     /*
    198      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    199      * Method:    brake
    200      * Signature: (JIF)F
    201      */
    202     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_brake
    203     (JNIEnv *env, jobject object, jlong vehicleId, jint wheel, jfloat value) {
    204         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    205         if (vehicle == NULL) {
    206             jclass newExc = env->FindClass("java/lang/NullPointerException");
    207             env->ThrowNew(newExc, "The native object does not exist.");
    208             return;
    209         }
    210         vehicle->setBrake(value, wheel);
    211     }
    212 
    213     /*
    214      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    215      * Method:    getCurrentVehicleSpeedKmHour
    216      * Signature: (J)F
    217      */
    218     JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_getCurrentVehicleSpeedKmHour
    219     (JNIEnv *env, jobject object, jlong vehicleId) {
    220         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    221         if (vehicle == NULL) {
    222             jclass newExc = env->FindClass("java/lang/NullPointerException");
    223             env->ThrowNew(newExc, "The native object does not exist.");
    224             return 0;
    225         }
    226         return vehicle->getCurrentSpeedKmHour();
    227     }
    228 
    229     /*
    230      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    231      * Method:    getForwardVector
    232      * Signature: (JLcom/jme3/math/Vector3f;)V
    233      */
    234     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_getForwardVector
    235     (JNIEnv *env, jobject object, jlong vehicleId, jobject out) {
    236         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    237         if (vehicle == NULL) {
    238             jclass newExc = env->FindClass("java/lang/NullPointerException");
    239             env->ThrowNew(newExc, "The native object does not exist.");
    240             return;
    241         }
    242         btVector3 forwardVector = vehicle->getForwardVector();
    243         jmeBulletUtil::convert(env, &forwardVector, out);
    244     }
    245 
    246     /*
    247      * Class:     com_jme3_bullet_objects_PhysicsVehicle
    248      * Method:    finalizeNative
    249      * Signature: (JJ)V
    250      */
    251     JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsVehicle_finalizeNative
    252     (JNIEnv *env, jobject object, jlong casterId, jlong vehicleId) {
    253         btVehicleRaycaster* rayCaster = reinterpret_cast<btVehicleRaycaster*>(casterId);
    254         btRaycastVehicle* vehicle = reinterpret_cast<btRaycastVehicle*>(vehicleId);
    255         if (vehicle == NULL) {
    256             jclass newExc = env->FindClass("java/lang/NullPointerException");
    257             env->ThrowNew(newExc, "The native object does not exist.");
    258             return;
    259         }
    260         delete(vehicle);
    261         if (rayCaster == NULL) {
    262             jclass newExc = env->FindClass("java/lang/NullPointerException");
    263             env->ThrowNew(newExc, "The native object does not exist.");
    264             return;
    265         }
    266         delete(rayCaster);
    267     }
    268 
    269 #ifdef __cplusplus
    270 }
    271 #endif
    272 
    273