Permalink
Browse files

Updated libgdx sources.

New ContactListener.preSolve/postSolve with all arguments available from Box2D.
  • Loading branch information...
1 parent 04cf2ce commit fd8e91f98710b5c7c1323e80a9f386b391ecc8f2 NicolasGramlich committed Jun 15, 2011
View
@@ -14,6 +14,8 @@ Box2D/MouseJoint.cpp \
Box2D/PrismaticJoint.cpp \
Box2D/PulleyJoint.cpp \
Box2D/RevoluteJoint.cpp \
+Box2D/ContactImpulse.cpp \
+Box2D/Manifold.cpp \
Box2D/Collision/b2BroadPhase.cpp \
Box2D/Collision/b2CollideCircle.cpp \
Box2D/Collision/b2CollidePolygon.cpp \
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
+ * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+#include "Box2D.h"
+#include "ContactImpulse.h"
+
+JNIEXPORT void JNICALL Java_com_badlogic_gdx_physics_box2d_ContactImpulse_jniGetNormalImpulses
+ (JNIEnv *env, jobject, jlong addr, jfloatArray values) {
+ b2ContactImpulse* contactImpulse = (b2ContactImpulse*)addr;
+
+ float* tmp = (float*)env->GetPrimitiveArrayCritical( values, 0 );
+ tmp[0] = contactImpulse->normalImpulses[0];
+ tmp[1] = contactImpulse->normalImpulses[1];
+
+ env->ReleasePrimitiveArrayCritical( values, tmp, 0 );
+}
+
+JNIEXPORT void JNICALL Java_com_badlogic_gdx_physics_box2d_ContactImpulse_jniGetTangentImpulses
+ (JNIEnv *env, jobject, jlong addr, jfloatArray values) {
+ b2ContactImpulse* contactImpulse = (b2ContactImpulse*)addr;
+
+ float* tmp = (float*)env->GetPrimitiveArrayCritical( values, 0 );
+ tmp[0] = contactImpulse->tangentImpulses[0];
+ tmp[1] = contactImpulse->tangentImpulses[1];
+
+ env->ReleasePrimitiveArrayCritical( values, tmp, 0 );
+}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
+ * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+#include "Box2D.h"
+#include "Manifold.h"
+
+JNIEXPORT jint JNICALL Java_com_badlogic_gdx_physics_box2d_Manifold_jniGetType
+ (JNIEnv *, jobject, jlong addr) {
+ b2Manifold* manifold = (b2Manifold*)addr;
+ return manifold->type;
+}
+
+JNIEXPORT jint JNICALL Java_com_badlogic_gdx_physics_box2d_Manifold_jniGetPointCount
+ (JNIEnv *, jobject, jlong addr) {
+ b2Manifold* manifold = (b2Manifold*)addr;
+ return manifold->pointCount;
+}
+
+JNIEXPORT void JNICALL Java_com_badlogic_gdx_physics_box2d_Manifold_jniGetLocalNormal
+ (JNIEnv *env, jobject, jlong addr, jfloatArray values) {
+ b2Manifold* manifold = (b2Manifold*)addr;
+ float* tmp = (float*)env->GetPrimitiveArrayCritical( values, 0 );
+ tmp[0] = manifold->localNormal.x;
+ tmp[1] = manifold->localNormal.y;
+ env->ReleasePrimitiveArrayCritical( values, tmp, 0 );
+}
+
+JNIEXPORT void JNICALL Java_com_badlogic_gdx_physics_box2d_Manifold_jniGetLocalPoint
+ (JNIEnv *env, jobject, jlong addr, jfloatArray values) {
+ b2Manifold* manifold = (b2Manifold*)addr;
+ float* tmp = (float*)env->GetPrimitiveArrayCritical( values, 0 );
+ tmp[0] = manifold->localPoint.x;
+ tmp[1] = manifold->localPoint.y;
+ env->ReleasePrimitiveArrayCritical( values, tmp, 0 );
+}
+
+JNIEXPORT jint JNICALL Java_com_badlogic_gdx_physics_box2d_Manifold_jniGetPoint
+ (JNIEnv *env, jobject, jlong addr, jfloatArray values, jint idx) {
+ b2Manifold* manifold = (b2Manifold*)addr;
+
+ float* tmp = (float*)env->GetPrimitiveArrayCritical( values, 0 );
+ tmp[0] = manifold->points[idx].localPoint.x;
+ tmp[1] = manifold->points[idx].localPoint.y;
+ tmp[2] = manifold->points[idx].normalImpulse;
+ tmp[3] = manifold->points[idx].tangentImpulse;
+ env->ReleasePrimitiveArrayCritical( values, tmp, 0 );
+
+ return (jint)manifold->points[idx].id.key;
+}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -84,7 +84,7 @@ class CustomContactListener: public b2ContactListener
virtual void BeginContact(b2Contact* contact)
{
if( beginContactID != 0 )
- env->CallVoidMethod(obj, beginContactID, (jlong)contact);
+ env->CallVoidMethod(obj, beginContactID, (jlong)contact );
}
/// Called when two fixtures cease to touch.
@@ -93,19 +93,19 @@ class CustomContactListener: public b2ContactListener
if( endContactID != 0 )
env->CallVoidMethod(obj, endContactID, (jlong)contact);
}
-
+
/// This is called after a contact is updated.
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
if( preSolveID != 0 )
- env->CallVoidMethod(obj, preSolveID, (jlong)contact);
+ env->CallVoidMethod(obj, preSolveID, (jlong)contact, (jlong)oldManifold);
}
-
+
/// This lets you inspect a contact after the solver is finished.
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
if( postSolveID != 0 )
- env->CallVoidMethod(obj, postSolveID, (jlong)contact);
+ env->CallVoidMethod(obj, postSolveID, (jlong)contact, (jlong)impulse);
}
};
@@ -139,8 +139,8 @@ JNIEXPORT jlong JNICALL Java_com_badlogic_gdx_physics_box2d_World_newWorld
worldClass = env->GetObjectClass(obj);
beginContactID = env->GetMethodID(worldClass, "beginContact", "(J)V" );
endContactID = env->GetMethodID( worldClass, "endContact", "(J)V" );
- preSolveID = env->GetMethodID( worldClass, "preSolve", "(J)V" );
- postSolveID = env->GetMethodID( worldClass, "postSolve", "(J)V" );
+ preSolveID = env->GetMethodID( worldClass, "preSolve", "(JJ)V" );
+ postSolveID = env->GetMethodID( worldClass, "postSolve", "(JJ)V" );
reportFixtureID = env->GetMethodID(worldClass, "reportFixture", "(J)Z" );
reportRayFixtureID = env->GetMethodID(worldClass, "reportRayFixture", "(JFFFFF)F" );
shouldCollideID = env->GetMethodID( worldClass, "contactFilter", "(JJ)Z");
View
Binary file not shown.
@@ -0,0 +1,29 @@
+package com.badlogic.gdx.physics.box2d;
+
+
+public class ContactImpulse {
+ final World world;
+ long addr;
+ float[] tmp = new float[2];
+ final float[] normalImpulses = new float[2];
+ final float[] tangentImpulses = new float[2];
+
+ protected ContactImpulse(World world, long addr) {
+ this.world = world;
+ this.addr = addr;
+ }
+
+ public float[] getNormalImpulses() {
+ jniGetNormalImpulses(addr, normalImpulses);
+ return normalImpulses;
+ }
+
+ private native void jniGetNormalImpulses(long addr, float[] values);
+
+ public float[] getTangentImpulses() {
+ jniGetTangentImpulses(addr, tangentImpulses);
+ return tangentImpulses;
+ }
+
+ private native void jniGetTangentImpulses(long addr, float[] values);
+}
@@ -16,23 +16,34 @@
package com.badlogic.gdx.physics.box2d;
public interface ContactListener {
- /**
- * Called when two fixtures begin to touch.
- */
- public void beginContact (Contact contact);
+ /**
+ * Called when two fixtures begin to touch.
+ */
+ public void beginContact (Contact contact);
- /**
- * Called when two fixtures cease to touch.
- */
- public void endContact (Contact contact);
+ /**
+ * Called when two fixtures cease to touch.
+ */
+ public void endContact (Contact contact);
+
+ /*
+ This is called after a contact is updated. This allows you to inspect a
+ contact before it goes to the solver. If you are careful, you can modify the
+ contact manifold (e.g. disable contact).
+ A copy of the old manifold is provided so that you can detect changes.
+ Note: this is called only for awake bodies.
+ Note: this is called even when the number of contact points is zero.
+ Note: this is not called for sensors.
+ Note: if you set the number of contact points to zero, you will not
+ get an EndContact callback. However, you may get a BeginContact callback
+ the next step. */
+ public void preSolve(Contact contact, Manifold oldManifold);
- /**
- * This is called after a contact is updated.
- */
- public void preSolve(Contact pContact);
-
- /**
- * This lets you inspect a contact after the solver is finished.
- */
- public void postSolve(Contact pContact);
+ /* This lets you inspect a contact after the solver is finished. This is useful
+ for inspecting impulses.
+ Note: the contact manifold does not include time of impact impulses, which can be
+ arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
+ in a separate data structure.
+ Note: this is only called for contacts that are touching, solid, and awake. */
+ public void postSolve(Contact contact, ContactImpulse impulse);
}
Oops, something went wrong.

0 comments on commit fd8e91f

Please sign in to comment.