collision response example

6 views
Skip to first unread message

makc

unread,
Jan 16, 2009, 1:45:08 PM1/16/09
to glaze Engine
hopefully it will help someone. in order to have collision events you
need:

1 extend rigidbody and implement ieventdispatcher in your class (I
read this was going to be done a year ago, but I havent find it)
2 override onWhateverCollision method
3 set corresponding collisionProcessingMask bit for this method to be
actually called (1 - start, 2 - collision, 4 - end, 7 - all)
4 dispatch event from that method.

after that, your code should look like:

package physext
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.getTimer;
import org.rje.glaze.engine.dynamics.RigidBody;

/**
* Simple collision response code.
*/
public class RigidBody2 extends RigidBody implements IEventDispatcher
{
private var lastTime:uint;
private var lastBody:RigidBody;
private var dispatcher:EventDispatcher;

public function RigidBody2 (type:int = DYNAMIC_BODY, m:Number = -1 ,
i:Number = -1) {
super (type, m, i); dispatcher = new EventDispatcher (this);

// subscribe to "onEndCollision" calls
collisionProcessingMask = 4;
}

override public function onEndCollision (body:RigidBody):void
{
if ((lastBody != body) || (getTimer () - lastTime > 2000))
{
lastBody = body;
lastTime = getTimer ();

var bonus:Number;
if (body.isStatic) {
// we hit obstacles: -100pts
bonus = -100;
} else {
if (v.magnitude () > body.v.magnitude ()) {
// we hit enemy: +300pts
bonus = +300;
} else {
// enemy hit us: -200pts
bonus = -200;
}
}

// fire RigidBody2Collision event
trace ("bonus: " + bonus);
dispatchEvent (new RigidBody2Collision (bonus));
}
}

// IEventDispatcher methods below
public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean
= false):void{
dispatcher.addEventListener(type, listener, useCapture, priority);
}

public function dispatchEvent(evt:Event):Boolean {
return dispatcher.dispatchEvent(evt);
}

public function hasEventListener(type:String):Boolean {
return dispatcher.hasEventListener(type);
}

public function removeEventListener(type:String, listener:Function,
useCapture:Boolean = false):void {
dispatcher.removeEventListener(type, listener, useCapture);
}

public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}

}

}

Iain

unread,
Jan 23, 2009, 5:49:35 AM1/23/09
to glaze Engine
I just went in and made RigidBody extend event dispatcher and chucked
the dispatches in there - probably reduces performance slightly but
only took me 5 minutes. Glaze is an awesome library, but seems to have
stopped developing. I wonder if Richard would accept new team members?
I wouldn't be much help with the physics parts but might be able to
help clean-up / extend the API?

rje

unread,
Jan 23, 2009, 8:15:35 AM1/23/09
to glaze Engine
Hi,

Thanks for you input/feedback.

I dont see why other people couldnt contribute - if you want to let me
know I'll add you as a team member. As for future developments:

- I initially wrote this engine to support the writing of a games. Of
course this took much more time and I never got round to building the
game.
- I'm planning on adding/releasing support for tiled worlds so that
large levels could be constructed with one of the tile level editors
out there. There would a special Tile type and broadphase to go with
it.
- I've also go some character/player physics type stuff to go in.
- Also, I've been play around with some steering behaviors, that would
allow a AI type character to navigate obstacles

These were all features of the game I was writing.. Anyway, if anybody
has other ideas them I'm interested. There are some parts that need
improving - anything performance-wise is always appreciated!
- Show quoted text -

Turbidity

unread,
Mar 2, 2009, 3:03:04 AM3/2/09
to glaze Engine
Thanks for the collision response example.

I thought I might help by explaining what I learned while getting it
to work.

I extended the RigidBody class with the new class LoudBody based on
your RigidBody2 class. (Loud because it's dispatching a bunch of
events.)
I'll only use LoudBody objects when they need collision responses.

package {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.getTimer;
import org.rje.glaze.engine.dynamics.RigidBody;


//Simple collision response code.

public class LoudBody extends RigidBody implements IEventDispatcher {
private var lastTime:uint;
private var lastBody:RigidBody;
private var dispatcher:EventDispatcher;

public function LoudBody(type:int = DYNAMIC_BODY, m:Number = -1,
i:Number = -1) {
super(type, m, i);
dispatcher = new EventDispatcher (this);

// subscribe to "onEndCollision" calls
collisionProcessingMask = 4;
}

override public function onEndCollision(body:RigidBody):void {
if ((lastBody != body) || (getTimer () - lastTime > 2000)) {
lastBody = body;
lastTime = getTimer ();

var bonk:String;
if (body.isStatic) {
// we hit obstacles: and it makes a "donk" sound
bonk = "donk";
} else {
if (v.magnitude () > body.v.magnitude ()) {
// we hit enemy: it dies
bonk = "kill";
} else {
// enemy hit us: we get hurt
bonk = "ouch";
}
}

// fire CollisionEvent event
trace("collisionType: " + bonk);
dispatchEvent(new CollisionEvent(bonk));
}
}

// IEventDispatcher methods below
public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean
= false):void {
dispatcher.addEventListener(type, listener, useCapture, priority);
}

public function dispatchEvent(evt:Event):Boolean {
return dispatcher.dispatchEvent(evt);
}

public function hasEventListener(type:String):Boolean {
return dispatcher.hasEventListener(type);
}

public function removeEventListener(type:String, listener:Function,
useCapture:Boolean = false):void {
dispatcher.removeEventListener(type, listener, useCapture);
}

public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}

}
}


Here is what might appear in the the CollisionEvent class, but

(for my example the RigidBody2Collision is called CollisionEvent.)


package {
import flash.events.Event;

public class CollisionEvent extends Event {
public static const DONK:String = "donk";
public static const OUCH:String = "ouch";
public static const KILL:String = "kill";
public var message:String;

public function CollisionEvent(message:String = "CollisionEvent") {
super(message);
this.message = message;
}
}
}

Meanwhile, somewhere in the class that instantiated the LoudBody:

import LoudBody;
import CollisionEvent
.........
loudBox();
}
private function loudBox():void {
var lbSize:Number = Math.random()*15 +5;
var lboxBody:LoudBody = new LoudBody(RigidBody.DYNAMIC_BODY,lbSize,
100);
lboxBody.p.setTo(Math.random()*600,0);
var boxShape:GeometricShape = new Polygon(Polygon.createRectangle
(lbSize, lbSize), Vector2D.zeroVect, wood);
lboxBody.addShape(boxShape);
lboxBody.canSleep = true;
space.addRigidBody(lboxBody);
lboxBody.addEventListener(CollisionEvent.DONK, donkHandler);
lboxBody.addEventListener(CollisionEvent.OUCH,
ouchHandler);
lboxBody.addEventListener(CollisionEvent.KILL,
killHandler);
}
private function donkHandler(e:Event):void {
trace(e.target, "got donked");
}
private function donkHandler(e:Event):void {
trace(e.target, "got hurt");
}
private function killHandler(e:Event):void {
trace(e.target, "kilt some guy");
}
..........
Reply all
Reply to author
Forward
0 new messages