Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How to decide if a sprite is close enough?

Author
Message
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 6th Nov 2008 23:34
I'm making a tower defense and was wondering how you could tell when the enemy sprite is close enough to the tower's range for it to attack.

Please explain what I have to do, don't just give me the solution straight away.

Charles
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 7th Nov 2008 00:19
You could use (maybe) a quicker method, calculating the tiles the tower can fire, and if the enemy is in that tile, fire at it...

or, the much easier method - checking distance using the Pythagorean theorem.
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 7th Nov 2008 17:21
lol

how does the Pythagorean theorem work?
prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 7th Nov 2008 21:43
to put it (kinda) simply, Pythagoras stated that you can work out any sides in a right angle triangle if you have 2 of the 3 sizes. so to work out the distance how far away the object is, you minus the turret x co-ord from the enemy co-ord (dont forget to "abs()" it) and the turret y co-ord from the enemy y co-ord and then use Pythagoras' theorem to work out the size of the third side (hypotenuse) and check to see if it is under a certain size
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 7th Nov 2008 23:11
Or...

x3 = dbABS(x1 - x2);
y3 = dbABS(y1 - y2);

distance = dbSQRT(x3 * x3 + y3 * y3 + z3 * z3);

Most people would put ()'s around the squaring, but that's useless, seeing as C++ sticks to the PEMDAS (order of operations) rule.

The sqrt() in the header "math.h" is faster than the GDK's.
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 8th Nov 2008 23:37
where does the z3 come from?
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 9th Nov 2008 09:27
take out the + z3 * z3 at the end.

p.s. you call it PEMDAS? We were taught BIDMAS.
brackets
indices
div/mult
add/sub
Charles Black
16
Years of Service
User Offline
Joined: 9th Mar 2008
Location:
Posted: 9th Nov 2008 13:15
And, what does dbABS do, exactly?
this is the abs() function I found through google:

jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 9th Nov 2008 20:25
an absolute value is a positive one. eg -5 becomes 5, -10 becomes 10 but 3 stays as 3
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 10th Nov 2008 00:46 Edited at: 10th Nov 2008 00:47
If you include math.h, you can do all of that more quickly.

That would give you the distance.

As for an absolute value, it's technically a value's distance from zero, which is therefore always positive. Just as speed, a distance lacks direction; They're scalar values. Unlike vector values (displacement, velocity, &c.) which have magnitude and direction, and can therefore be negative.


My site, for various stuff that I make.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 10th Nov 2008 20:38
You can remove the dbABS command, it is not needed here, since for any value of 'x', x*x is positive anyway!

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
RomuluS85
18
Years of Service
User Offline
Joined: 10th Apr 2006
Location:
Posted: 17th Nov 2008 00:29
umm... all of the above and the sqrt always returns a positive value (touché)

Login to post a reply

Server time is: 2024-09-30 11:25:05
Your offset time is: 2024-09-30 11:25:05