Forum Topic: AS: Angled Distance

(1,602 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

23450

Reply To Post Reply & Quote

Posted at: 5/20/06 04:28 PM

23450 LIGHT LEVEL 27

Sign-Up: 05/28/03

Posts: 7,304

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.

Halo 3 GamerTag: XCornDawgX


None

zoy

Reply To Post Reply & Quote

Posted at: 5/20/06 04:29 PM

zoy EVIL LEVEL 09

Sign-Up: 05/17/04

Posts: 1,383

very thorough and complete. A work of art in text, methinks. I must say, i propably will use this pretty soon.

BBS Signature

Happy

Depredation

Reply To Post Reply & Quote

Posted at: 5/20/06 04:32 PM

Depredation LIGHT LEVEL 17

Sign-Up: 09/05/05

Posts: 4,783

Nice, very well explained and original. A very handy AS. Denvish will add it soon.

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 5/20/06 04:38 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

BBS Signature

None

23450

Reply To Post Reply & Quote

Posted at: 5/20/06 04:39 PM

23450 LIGHT LEVEL 27

Sign-Up: 05/28/03

Posts: 7,304

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.

Halo 3 GamerTag: XCornDawgX


None

fuzz

Reply To Post Reply & Quote

Posted at: 5/20/06 04:42 PM

fuzz NEUTRAL LEVEL 21

Sign-Up: 06/02/04

Posts: 5,647

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


None

Rantzien

Reply To Post Reply & Quote

Posted at: 5/20/06 04:58 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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.

BBS Signature

None

23450

Reply To Post Reply & Quote

Posted at: 5/20/06 05:53 PM

23450 LIGHT LEVEL 27

Sign-Up: 05/28/03

Posts: 7,304

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.

Halo 3 GamerTag: XCornDawgX


None

trig1

Reply To Post Reply & Quote

Posted at: 1/13/07 10:25 AM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

the working file has a broken link

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 1/13/07 10:29 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,380

That's because you bumped t by seven months.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

trig1

Reply To Post Reply & Quote

Posted at: 1/13/07 10:53 AM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

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

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

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 04:28 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!