 00:00
00:00 AS: Angled Distance

2,931 Views | 10 Replies

AS: Angled Distance 2006-05-20 16:28:17

AS: Main

FlashTroops

This tutorial will explain a code that allows you to determine the distance that a movieclip is from any surface, according to the angle you happen to be facing. It involves some trig functions, a “for” loop, and shapeflags. Here is the full code:

onClipEvent (enterFrame) {
xDist = _x-_root._xmouse;
yDist = _y-_root._ymouse;
angle = Math.atan2(yDist, xDist)/(Math.PI/180)-180;
xDiff = Math.cos(angle*(Math.PI/180))*1;
yDiff = Math.sin(angle*(Math.PI/180))*1;
for (i=0; i<600; i++) {
xComp = Math.cos(angle*(Math.PI/180))*i+_x;
yComp = Math.sin(angle*(Math.PI/180))*i+_y;
if (_root.ground.hitTest(xComp, yComp, true)) {
if (_root.ground.hitTest(xComp-xDiff, yComp-yDiff, true) == false) {
if (_root.ground.hitTest(xComp+xDiff, yComp+yDiff, true)) {
_root.distance = i;
}
}
}
}
}

Working File

Explaination File

The code will work when placed on a movieclip, and will calculate your distance from the “ground” movieclip. Now for the explainations.

xDist = _x-_root._xmouse;
yDist = _y-_root._ymouse;
angle = Math.atan2(yDist, xDist)/(Math.PI/180)-180;

These first three lines are what calculate the angle between the movieclip and the mouse. The distance we are looking for from the ground movieclip is along this angle.

xDiff = Math.cos(angle*(Math.PI/180))*1;
yDiff = Math.sin(angle*(Math.PI/180))*1;

This is the number that we will later use within the shapeflag to help us determine the exact distance. The reason for this code will be further explained later in the tutorial.

for (i=0; i<600; i++) {

This is the loop we will be using for the calculations. 600 for a loop is a little much, but for this tutorial, it is what I will be using to gain the correct distance, to the pixel. If you want to change the ++ to anything other then +=1 (eg. +=2, +=3, +=4) you will have to change the “1” at the end of the x and y Diff codes.

xDiff = Math.cos(angle*(Math.PI/180))*1 ;
yDiff = Math.sin(angle*(Math.PI/180))*1 ;

Change the 1 to whatever number you want to loop to go up by. Raising the number will create less lag, but the calculations will not be as accurate.

xComp = Math.cos(angle*(Math.PI/180))*i+_x;
yComp = Math.sin(angle*(Math.PI/180))*i+_y;

These calculate the X and Y components of the angle. The distance of these are controlled by the loop.

if (_root.ground.hitTest(xComp, yComp, true)) {
if (_root.ground.hitTest(xComp-xDiff, yComp-yDiff, true) == false) {
if (_root.ground.hitTest(xComp+xDiff, yComp+yDiff, true)) {

This is the actual calculations. These three lines will calculate the distance between the ground movieclip and the movieclip with this code on it. If one of the components is touching the ground, and that component-1 (x and y Diff is equal to 1) is not touching, and component +1 is touching, then calculate the distance.

This code can be used in a number of situations. If you want a sidescroller or an overhead shooter, and did not want to use duplicated movieclips as bullets, this code could create instantaneous shooting, like a real gun would. Maybe for dynamic lighting on a bumpy surface, and plenty of other things im sure. Hope somebody learned something.

Response to AS: Angled Distance 2006-05-20 16:29:52

very thorough and complete. A work of art in text, methinks. I must say, i propably will use this pretty soon. Response to AS: Angled Distance 2006-05-20 16:32:59

Nice, very well explained and original. A very handy AS. Denvish will add it soon. Response to AS: Angled Distance 2006-05-20 16:38:22

That's a damn lot of hitTests. Use bigger intervals (say 10-50), then when it hits you can check everywhere between the last non-hit position and the first hit position.

Example:

Dist 0: No hit
Dist 30: No hit
Dist 60: No hit
Dist 90: Hit
(Check between 60 and 90)
Dist 70: No hit
Dist 80: Hit
(Check between 70 and 80)
Dist 71: No hit
Dist 72: No hit
Dist 73: Hit Response to AS: Angled Distance 2006-05-20 16:39:42

At 5/20/06 04:38 PM, Rantzien wrote: That's a damn lot of hitTests. Use bigger intervals (say 10-50), then when it hits you can check everywhere between the last non-hit position and the first hit position.

I already explained that in the tutorial. I said i used 1 as the interval in the tut for pure accuracy. I explain that if you want to change it to a higher number you aldo have to change x and y Diff. So yah, i already thought of that.

Response to AS: Angled Distance 2006-05-20 16:42:56

so could you use this say on a shooting game,
so if the enemy is >10 away you get shot type1 were its more accurate
and if enemy is <10<100 you get the next type and so on..
thats cool

Response to AS: Angled Distance 2006-05-20 16:58:11

At 5/20/06 04:39 PM, 23450 wrote: I already explained that in the tutorial. I said i used 1 as the interval in the tut for pure accuracy. I explain that if you want to change it to a higher number you aldo have to change x and y Diff. So yah, i already thought of that.

Yes, but you could've explained having big intervals, then decreasing them. It could speed up the code by 50 times but preserve the accuracy. Response to AS: Angled Distance 2006-05-20 17:53:40

At 5/20/06 04:58 PM, Rantzien wrote: Yes, but you could've explained having big intervals, then decreasing them. It could speed up the code by 50 times but preserve the accuracy.

ah, i get it now. Yah, that probably would be a good idea.

Response to AS: Angled Distance 2007-01-13 10:25:19

the working file has a broken link

Response to AS: Angled Distance 2007-01-13 10:29:41

That's because you bumped t by seven months. Response to AS: Angled Distance 2007-01-13 10:53:34

At 1/13/07 10:29 AM, OldGust wrote: That's because you bumped t by seven months.

...........maybe.