QuickBox2D Contacts Part 2

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2.  
  3. [SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate=60)]
  4.  
  5. var sim:QuickBox2D = new QuickBox2D(this);
  6.  
  7. sim.createStageWalls();
  8.  
  9. var boxA:QuickObject = sim.addBox({x:3, y:3, width:2, height:2});
  10. var boxB:QuickObject = sim.addBox({x:3, y:6, width:2, height:2});
  11.  
  12. // add a few extra circles to show that there are other contacts
  13. // occuring
  14. for (var i:int = 0; i<10; i++){
  15.     sim.addCircle({x:5 + i, y:4, radius:0.1 + Math.random()});
  16. }
  17.  
  18. sim.start();
  19. sim.mouseDrag();
  20.  
  21. // when boxA touches boxB change their alpha values to 50%
  22. var contacts:QuickContacts = sim.addContactListener();
  23. contacts.addEventListener(QuickContacts.ADD, onAdd);
  24. contacts.addEventListener(QuickContacts.PERSIST, onPersist);
  25. contacts.addEventListener(QuickContacts.REMOVE, onRemove);
  26. function onAdd(evt:Event):void{
  27.     if(contacts.isCurrentContact(boxA, boxB)){
  28.         boxA.userData.alpha = 0.5;
  29.         boxB.userData.alpha = 0.5;
  30.     }
  31. }
  32. function onPersist(evt:Event):void{
  33.     if(contacts.isCurrentContact(boxA, boxB)){
  34.         boxA.userData.alpha = 0.5;
  35.         boxB.userData.alpha = 0.5;
  36.     }
  37. }
  38. function onRemove(evt:Event):void{
  39.     if(contacts.isCurrentContact(boxA, boxB)){
  40.         boxA.userData.alpha = 1;
  41.         boxB.userData.alpha = 1;
  42.     }
  43. }

Note: This snippet requires QuickBox2D 1.0 or greater

This is another example showing how to make use of QuickBox2D contacts. In this example three contact events are used ADD, PERSIST and REMOVE. When the two boxes are touching their alpha values are set to 50% - otherwise their alpha values are at 100%.


Have a look at the swf...

A Little Detail About the Events

The ADD event is dispatched when a new b2ContactPoint is generated. The PERSIST event is dispatched as long as the b2ContactPoint exists for more than one timeStep - and from what I can tell, is dispatched until the rigid bodies it is associated with go to sleep. The REMOVE event is called when the b2ContactPoint is removed, meaning the rigid bodies that caused the creating of the b2ContactPoint are no longer touching.

You need to be careful with the code that you place inside the listener functions for these events. The reason being that these events are called many many times for every contact point in the simulation.

This entry was posted in Box2D, QuickBox2D and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Khirod
    Posted January 17, 2011 at 5:41 am | Permalink

    Hi
    How to count the number of object (ball etc) drop in dynamic cups , there may more than one cup in a onchange event of a combox.

    Please guide me how to do this ?

    khirod

One Trackback

  1. [...] QuickBox2Dによる衝突判定2 – QuickBox2D Contacts Part 2 [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*