Contact vs Collision; AndEngine vs Box2D physics

  ... the Physics Box2D Extension for AndEngine.

Contact vs Collision; AndEngine vs Box2D physics

Postby D13 » Wed Oct 06, 2010 6:35 am

I'm in the process of trying to teach myself this stuff, working with a test application that I'm gradually throwing in different features of the AndEngine examples to better understand how it all works and explore the possibilities. I'm having a bit of difficultly making sense of Contacts and Collisions, and AndEngine vs Box2D physics as I've seen discussed in a few topics here. Is it possible that someone might provide a quick description the difference between contacts and collisions, when is most appropriate to use them, are they supposed to be used together, separately, or does it depend on the situation, and what features make AndEngine physics difference than Box2D physics.

It it helps, I "think" I'm using Box2D physics at the moment. My test app is shooting objects around the screen from a "gun" sprite, these projectile sprites/bodies are bouncing off of other animated sprites all responding to gravity, velocity, friction, (fixturedef stuff). But it's not clear to me exactly what I should be trying to use to detect and process when these objects "interact" with each other (what type of interaction I'm seeing). Do bodies collide and sprites contact?

I'm also noticing that, when run on the emulator, when the projectile is moving quite quickly, it apparently seems to pass straight through another object, but when slower, it interacts normally with the object. Is it perhaps because the update speed is slow enough that the object's calculated position manages to move from one side to the other side of the other object before any physics interaction can be detected and acted upon?
D13
 
Posts: 172
Joined: Wed Oct 06, 2010 6:08 am
Location: Toronto, Canada

Re: Contact vs Collision; AndEngine vs Box2D physics

Postby Nicolas Gramlich » Sun Oct 10, 2010 7:11 pm

Hi,

Box2D uses the term "contact" when two bodies are in contact (touch or overlap) with each other. AndEngine can only detect overlapping rectangles but not calculate the bouncing etc.. . So you cannot use AndEngine without Box2D to simulate physics as AndEngine is no physics engine, but Box2D is :!:

To detect contacts between objects you'd do this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         this.mPhysicsWorld.setContactListener(new ContactListener() {
  2.                 @Override
  3.                 public void beginContact(final Contact pContact) {
  4.                         /* Do something... */
  5.                 }
  6.  
  7.                 @Override
  8.                 public void endContact(final Contact pContact) {
  9.                         /* Do something... */
  10.                 }
  11.         });
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


The "passing through" is a problem of any simulation. When the timestep gets to long (or the velocity to high), the physics-engine can only see that the objects was in front in the first frame and in the second frame it was already behind the other object. So did it hit the other object :?: ... maybe it did :roll:

Hope this helped :)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Contact vs Collision; AndEngine vs Box2D physics

Postby Wiese82 » Mon Oct 11, 2010 12:21 pm

Hi!

when the projectile is moving quite quickly, it apparently seems to pass straight through another object, but when slower, it interacts normally with the object.


I think for this problem you can use the "bullet feature" for dynamic bodies:
Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
  1. void SetBullet(bool flag);
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Further information in the official Box2D manual (http://www.box2d.org/manual.html) or here ;-)

Bullets

Game simulation usually generates a sequence of images that are played at some frame rate. This is called discrete simulation. In discrete simulation, rigid bodies can move by a large amount in one time step. If a physics engine doesn't account for the large motion, you may see some objects incorrectly pass through each other. This effect is called tunneling.

By default, Box2D uses continuous collision detection (CCD) to prevent dynamic bodies from tunneling through static bodies. This is done by sweeping shapes from their old position to their new positions. The engine looks for new collisions during the sweep and computes the time of impact (TOI) for these collisions. Bodies are moved to their first TOI and then halted for the remainder of the time step.

Normally CCD is not used between dynamic bodies. This is done to keep performance reasonable. In some game scenarios you need dynamic bodies to use CCD. For example, you may want to shoot a high speed bullet at a stack of dynamic bricks. Without CCD, the bullet might tunnel through the bricks.

Fast moving objects in Box2D can be labeled as bullets. Bullets will perform CCD with both static and dynamic bodies. You should decide what bodies should be bullets based on your game design. If you decide a body should be treated as a bullet, use the following setting.

bodyDef.bullet = true;

The bullet flag only affects dynamic bodies.

Box2D performs continuous collision sequentially, so bullets may miss fast moving bodies.


Greets,
Wiese82
Download my new game: Safari! / Safari! HD
Wiese82
 
Posts: 45
Joined: Sun Jul 18, 2010 7:00 pm
Location: Germany

Re: Contact vs Collision; AndEngine vs Box2D physics

Postby oleg » Mon Oct 11, 2010 3:25 pm

AndEngine is doing a great job detecting overlapping all kinds of shapes, not only rectangles, when given an array of vertices :)
oleg
 
Posts: 39
Joined: Mon Jul 12, 2010 11:15 am

Re: Contact vs Collision; AndEngine vs Box2D physics

Postby D13 » Fri Oct 15, 2010 2:05 am

Thanks for the answers. It's all starting to make some sense to me. When I originally came across AndEngine, it was not immediately clear to me that Box2d was actually a separate open source project. Now, reading their manuals and such helped me to sort out whats going on and likewise, what AndEngine is doing. I now realize that physics in AndEngine is actually 3 layers of work. Box2D providing the physics engine, BadLogic providing the JNI interface for the Box2D C++ code, and AngEngine providing pretty much everything else to make it nice on Android. That brings me to one last "overall concept" question. I was reading up on JNI on the Google Android site, and they made some "Oh, you might not want to do that" type comments because the code uses specific instruction sets which might not be compatible with all devices. So far it works on my HTC Desire and on the emulator, but I'm wondering what should be the extend of my concern over this? Are there different specific JNI libraries for different devices (ie do I need to build different versions of the app for different devices). I've come across a few apps in the Market that seem to have multiple versions for different devices...
D13
 
Posts: 172
Joined: Wed Oct 06, 2010 6:08 am
Location: Toronto, Canada

Re: Contact vs Collision; AndEngine vs Box2D physics

Postby Samdekkol » Thu Aug 13, 2015 8:52 pm

I think that this information is the best.
Samdekkol
 
Posts: 15
Joined: Thu Aug 13, 2015 7:21 pm


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 6 guests