Box2d Raycast

  ... the case you feel the need for a new feature or want to submit one.

Box2d Raycast

Postby HexWave » Mon Dec 13, 2010 4:37 am

Hi,

I have patched AndEnginePhysicsBox2DExtension to support Box2d raycasts. I haven't implemented casting a body yet, but this allows you to cast a ray from a point and get callbacks with Fixtures.

I have attached a zip containing the JAR files and compiled .so file that you can drop into your project for RayCast support. This also includes a patched AndEngine jar to fix an issue with dropped Runnables and added an unloadBufferObjects() method to the Text class that I've posted about in the Bugs section.

I will add more details here as I pull things together.

I've also attached the patch that adds raycast support. Parts of this should be pushed upstream to gdx.

Once the patch is applied, run ndk-build from the Android NDK in the jni/ directory to create a new libs/armeabi/libandenginephysicsbox2dextension.so library. Also build a new JAR of the AndEnginePhysicsBox2dExtension source.

There is a new RayCastCallback class that will be called when calling the RayCast method on your PhysicsWorld. The return value of your overridden reportRayFixture method will determine if the raycast is terminated or continues as explained in the box2d documentation.

Perform a raycast like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. RayCastCallback cb = new RayCastCallback() {
  2.  
  3.   @Override
  4.     public float reportRayFixture(Fixture pFixture, Vector2 pPoint,
  5.                                                 Vector2 pNormal, float pFraction) {
  6.       Log.v("RayCast", "RAYCAST " + pPoint.toString() + " " + pNormal.toString() + " " + pFraction);
  7.       return 0;
  8.     }
  9. };
  10.    
  11. mPhysicsWorld.RayCast(cb, new Vector2(worldStartX, worldStartY), new Vector2(worldEndX,worldEndY));
  12.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


Cheers,
Matt
HexWave
Attachments
andengine_box2d_raycast.zip
Compiled AndEngine and Box2d libraries with RayCast support
(603.5 KiB) Downloaded 309 times
raycast.zip
Box2d Raycast Patch
(2.38 KiB) Downloaded 302 times
Last edited by HexWave on Mon Dec 13, 2010 8:22 am, edited 2 times in total.
HexWave
 
Posts: 22
Joined: Sat Nov 13, 2010 12:12 am

Re: Box2d Raycast

Postby samgreen » Mon Dec 13, 2010 7:05 am

Thanks very much, will post with results.
samgreen
 
Posts: 36
Joined: Thu Dec 02, 2010 4:35 am

Re: Box2d Raycast

Postby badlogicgames » Wed Dec 22, 2010 4:20 pm

The patch for the libgdx parts looks good except for one thing:

Code: Select all
   public Vector2 getLinearVelocityFromWorldPoint (Vector2 worldPoint) {
+      Vector2 linVelWorld = new Vector2();


Any reason why you switched from using the final member to instantiating a new Vector2 each time?

It would also be nice if you guys could inform me of such things so i can add it to upstream. I don't know which revision of the wrapper Nicolas is currently using but Andengine can always benefit from using the latest and greatest.

Thanks for the patch HexWave!

edit: well, the patch misses the RaycastCallback interface. I added it to upstream. Dunno whether Nicolas will include that...
badlogicgames
 
Posts: 19
Joined: Sat Jul 10, 2010 5:27 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 4 guests