View Full Version : Collision detection?
whisperstorm
09-24-2003, 01:49 PM
Ok I'm taking off my work hat for a moment and putting on my play hat. I'm putting together a simple shooter game (like asteroids) and I'm thinking that with all the custom event stuff, as well as constraint based attributes that there surely is a simple, straightforward way to do collision detection. Am I right? Well I'm going to do some experimentation, but if anyone else already has something figured out, I'd love to take a look at the lzx...
antun
09-24-2003, 04:11 PM
Here's a quick example to get you started:
<canvas debug="true">
<view name="projectile" bgcolor="red" width="20" height="20" y="50"
clickable="true">
<attribute name="collisionValue" value="$once{wall.x}" />
<method event="onclick">
this.moveRight();
</method>
<method name="moveRight">
this.setX( this.x + 4 );
if ( this.checkForCollision() ) {
debug.write( 'Collision!!!!' );
return;
}
this.setMoveAgainDelegate();
</method>
<method name="setMoveAgainDelegate">
this.moveDelegate = new LzDelegate( this, "moveRight" );
LzTimer.addTimer( this.moveDelegate, 10 );
</method>
<method name="checkForCollision">
<![CDATA[
return this.x + this.width >= this.collisionValue;
]]>
</method>
</view>
<view name="wall" bgcolor="blue" width="5" height="300" x="300" />
</canvas>
Click the red box to begin. If you were going to get a bit more complicated, you might write an abstract collision manager class, that had a register() that each particle would call with itself as the argument when it was created, so it was aware of all the bits floating around. Then you'd have that manager check the location of each registered particle, to see if there were any collisions.
You might have the manager call its "check" method onidle (constantly), or you might have its register() method register delegates with the onx and ony events of each particle. I'm not sure which would be more efficient.
It would be better to do this using animators, but right now there's no way to suddenly stop an animator, so you wouldn't be able to interrupt a particle's travel.
-Antun
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.