Community

collision detection

Posted Oct 24, '09 at 6:32pm

rino

rino

26 posts

Wood - Squire

I have found this code in as2 and I am trying to make it have collision detection, with objects of different shapes but have been unable to do so. Does anyone know how to do this.

onClipEvent (load) {
theta = 0;
thrust = 0;
angle_inc = 5;
vx = 0;
vy = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
thrust = .1;
} else {
thrust = 0;
}
if (Key.isDown(Key.LEFT)) {
theta = theta-angle_inc;
} else if (Key.isDown(Key.RIGHT)) {
theta = theta+angle_inc;
}
if (Key.isDown(Key.DOWN)) {
thrust -= .1;

}
vx += thrust*Math.cos(theta/180*Math.PI);
vy += thrust*Math.sin(theta/180*Math.PI);
this._x += vx;
this._y += vy;

this._rotation = theta;
}

Posted Oct 24, '09 at 7:57pm

plasmafish

plasmafish

36 posts

Iron - Serf

onClipEvent (enterFrame) {
if (this.hitTest(_root.item)) {
CODE GOES HERE
}
}

Do you understand how to use this simple code?

 

Posted Oct 25, '09 at 2:08pm

rino

rino

26 posts

Wood - Squire

yes and i have tried this, but it does not serve the purpose of what is trying to be done.

do you understand what is trying to be done?

 

Posted Oct 26, '09 at 9:01am

plasmafish

plasmafish

36 posts

Iron - Serf

Yep, looks like Asteroid code to me.

 

Posted Oct 26, '09 at 9:04am

plasmafish

plasmafish

36 posts

Iron - Serf

Maybe you need to break apart the objects and refine the edges.

 

Posted Oct 29, '09 at 9:16pm

ZeroComp

ZeroComp

79 posts

Gold - Squire

I have tried it and it does seem to work but the one plasmafish wrote didn't work

 

Posted Oct 30, '09 at 8:29am

plasmafish

plasmafish

36 posts

Iron - Serf

The one I wrote was taken directly from a movie I had made.  You have to apply it to the movie clip that you want to interact with the moving character/object.  _root.item would be changed to something like bullet or collideobject, _root is not needed in all scenarios, I just used it in my case.

Where it says CODE GOES HERE, you would pass variables and do whatever you need the collision to do (applying to the first set of code also if needed).

 
Reply to collision detection

You must be logged in to post a reply!