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#include "com_jme3_bullet_collision_PhysicsCollisionObject.h"
37#include "jmeBulletUtil.h"
38#include "jmePhysicsSpace.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44    /*
45     * Class:     com_jme3_bullet_collision_PhysicsCollisionObject
46     * Method:    attachCollisionShape
47     * Signature: (JJ)V
48     */
49    JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_attachCollisionShape
50    (JNIEnv * env, jobject object, jlong objectId, jlong shapeId) {
51        btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
52        if (collisionObject == NULL) {
53            jclass newExc = env->FindClass("java/lang/IllegalStateException");
54            env->ThrowNew(newExc, "The collision object does not exist.");
55            return;
56        }
57        btCollisionShape* collisionShape = reinterpret_cast<btCollisionShape*>(shapeId);
58        if (collisionShape == NULL) {
59            jclass newExc = env->FindClass("java/lang/IllegalStateException");
60            env->ThrowNew(newExc, "The collision shape does not exist.");
61            return;
62        }
63        collisionObject->setCollisionShape(collisionShape);
64    }
65
66    /*
67     * Class:     com_jme3_bullet_collision_PhysicsCollisionObject
68     * Method:    finalizeNative
69     * Signature: (J)V
70     */
71    JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_finalizeNative
72    (JNIEnv * env, jobject object, jlong objectId) {
73        btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
74        if (collisionObject == NULL) {
75            jclass newExc = env->FindClass("java/lang/NullPointerException");
76            env->ThrowNew(newExc, "The native object does not exist.");
77            return;
78        }
79        if (collisionObject -> getUserPointer() != NULL){
80            jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
81            delete(userPointer);
82        }
83        delete(collisionObject);
84    }
85    /*
86     * Class:     com_jme3_bullet_collision_PhysicsCollisionObject
87     * Method:    initUserPointer
88     * Signature: (JII)V
89     */
90    JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_initUserPointer
91      (JNIEnv *env, jobject object, jlong objectId, jint group, jint groups) {
92        btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
93        if (collisionObject == NULL) {
94            jclass newExc = env->FindClass("java/lang/NullPointerException");
95            env->ThrowNew(newExc, "The native object does not exist.");
96            return;
97        }
98        jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
99        if (userPointer != NULL) {
100//            delete(userPointer);
101        }
102        userPointer = new jmeUserPointer();
103        userPointer -> javaCollisionObject = env->NewWeakGlobalRef(object);
104        userPointer -> group = group;
105        userPointer -> groups = groups;
106        userPointer -> space = NULL;
107        collisionObject -> setUserPointer(userPointer);
108    }
109    /*
110     * Class:     com_jme3_bullet_collision_PhysicsCollisionObject
111     * Method:    setCollisionGroup
112     * Signature: (JI)V
113     */
114    JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollisionGroup
115      (JNIEnv *env, jobject object, jlong objectId, jint group) {
116        btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
117        if (collisionObject == NULL) {
118            jclass newExc = env->FindClass("java/lang/NullPointerException");
119            env->ThrowNew(newExc, "The native object does not exist.");
120            return;
121        }
122        jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
123        if (userPointer != NULL){
124            userPointer -> group = group;
125        }
126    }
127    /*
128     * Class:     com_jme3_bullet_collision_PhysicsCollisionObject
129     * Method:    setCollideWithGroups
130     * Signature: (JI)V
131     */
132    JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollideWithGroups
133      (JNIEnv *env, jobject object, jlong objectId, jint groups) {
134        btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
135        if (collisionObject == NULL) {
136            jclass newExc = env->FindClass("java/lang/NullPointerException");
137            env->ThrowNew(newExc, "The native object does not exist.");
138            return;
139        }
140        jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
141        if (userPointer != NULL){
142            userPointer -> groups = groups;
143        }
144    }
145
146#ifdef __cplusplus
147}
148#endif
149