Tag Archives: as3

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.

Posted in Box2D, QuickBox2D | Also tagged , , , | 2 Comments

QuickBox2D Contacts Part 1

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. [SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate=60)]
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this, {debug:false, frim:true});
  7.  
  8. sim.createStageWalls();
  9.  
  10. var boxA:QuickObject = sim.addBox({x:3, y:3, width:2, height:2, fillColor:0xFF0000});
  11. var boxB:QuickObject = sim.addBox({x:3, y:6, width:2, height:2, fillColor:0xFF0000});
  12.  
  13. sim.start();
  14. sim.mouseDrag();
  15.  
  16. // when boxA touches boxB a circle QuickObject is created
  17. var contacts:QuickContacts = sim.addContactListener();
  18. // listen for contact points being added
  19. contacts.addEventListener(QuickContacts.ADD, onAdd);
  20. function onAdd(evt:Event):void{
  21.     // see if this contact event is associated with boxA and boxB
  22.     if(contacts.isCurrentContact(boxA, boxB)){
  23.         // get the location of the collision in world space
  24.         var loc:b2Vec2 = contacts.currentPoint.position;
  25.         // you cannot create new QuickObjects inside this listener function
  26.         // so we just give a 5 ms delay
  27.         setTimeout(sim.addCircle, 5, {x:loc.x, y:loc.y});
  28.     }
  29. }

Note: This snippet requires QuickBox2D 1.0 or greater

I've created a few simple examples to show how to use QuickBox2D contact listeners. This is the first one. When two boxes collide circles are added to the simulation at the point of collision.


Have a look at the swf...

Posted in Box2D, QuickBox2D | Also tagged , , , | 10 Comments

QuickBox2D 1.0

QuickBox2D 1.0 is ready. It contains a bunch of small bug fixes and a few new features that I'll be demoing in the next few posts. The main new features relate to collision detection and simple event sequencing.

Go download it from here...

I also spent some time updating the docs.... everything is documented... I may add additional documentation text over the next few days...

A detailed tutorial for QuickBox2D is also in the works...

[EDIT]

Also, if you find any bugs send me an e-mail (see about page for e-mail)... if there are any bugs left I'd like to fix the asap.... You can also feel free to e-mail any API suggestions...

Posted in Box2D, QuickBox2D | Also tagged , , , | 2 Comments

drawTriangles() Cubes

Actionscript:
  1. private function addCube(xp:Number, yp:Number, zp:Number, w:Number, h:Number, leng:Number):void{
  2.             var hw:Number = w * 0.5;
  3.             var hh:Number = h * 0.5;
  4.             var hl:Number = leng * 0.5;
  5.             var xA:Number = xp - hw;
  6.             var xB:Number = hw + xp;
  7.             var yA:Number = yp - hh;
  8.             var yB:Number = hh + yp;
  9.             var zA:Number = zp - hl;
  10.             var zB:Number = hl + zp;
  11.             _verts.push(xA, yA, zA,
  12.                         xB, yA, zA,
  13.                         xA, yB, zA,
  14.                         xB, yB, zA,
  15.                         xA, yA, zB,
  16.                         xB, yA, zB,
  17.                         xA, yB, zB,
  18.                         xB, yB, zB);
  19.            
  20.             var index:int = _boxIndex * 8;
  21.             var i0:int = index, i1:int = index + 1, i2:int = index + 2;
  22.             var i3:int = index + 3,  i4:int = index + 4, i5:int = index + 5;
  23.             var i6:int = index + 6, i7:int = index + 7;
  24.             _indices.push(i0, i1, i2,
  25.                           i1, i3, i2,
  26.                           i6, i7, i4,
  27.                           i7, i5, i4,
  28.                           i1, i5, i3,
  29.                           i7, i3, i5,
  30.                           i4, i5, i0,
  31.                           i1, i0, i5,
  32.                           i2, i6, i0,
  33.                           i0, i6, i4,
  34.                           i2, i3, i6,
  35.                           i3, i7, i6);
  36.                          
  37.             _faces.push(new Face(), new Face(), new Face(),
  38.                         new Face(),  new Face(), new Face(),
  39.                         new Face(), new Face(), new Face(),
  40.                         new Face(), new Face(), new Face());
  41.             _uvts.push(Math.random(), Math.random(), 0,
  42.                        Math.random(), Math.random(), 0,
  43.                        Math.random(), Math.random(), 0,
  44.                        Math.random(), Math.random(), 0,
  45.                        Math.random(), Math.random(), 0,
  46.                        Math.random(), Math.random(), 0,
  47.                        Math.random(), Math.random(), 0,
  48.                        Math.random(), Math.random(), 0);
  49.             _boxIndex++;
  50.         }

Lately I've been posting large code snippets... so today I'm highlighting part of a larger snippet - The above code is the heart of a small experiment I created this morning. It sets up a cube for use with drawTraingles().

The rest of the code can be read here:
Cubes3D.as

Have a look at the swf here...


I also put it on wonderfl...

Posted in 3D, Graphics, Vector, matrix, motion | Also tagged , | 6 Comments