• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Traceline, Convert to 3D. Most 2D code already done.

Status
Not open for further replies.
Level 1
Joined
Feb 1, 2009
Messages
5
Hey everyone its been awhile since I've been on this site, as I stopped coding on starcraft/warcraft awhile ago.. But starcraft II came out, and I would like to start over with my whole FPS.. (lag I know, just making the map for now)

I have a 2D trace-line that I got from here, I did my research and stumbled upon it, everyone else is using the 50 or 100 loop method.

Anyways, would someone be able to provide me with the code that will get the height the trace-line intersects with the picked unit?
Here is the thread containing the code.

http://www.hiveworkshop.com/forums/...zone-647/simple-2d-traceline-function-167915/
 
Level 12
Joined
Apr 15, 2008
Messages
1,063
Yes it's the same algorithm (good choice using this one over the 100 loop one :grin: ).

Testing height is very easy. You will need to calculate distance for each unit, and from it the allowed heights. Distance between attacker (camera) and the unit is easy, of course you don't need to use the default action, as it uses sqrt, which is very inefficient.
I suppose you use mrzwatch's version:
Use the a and b variables to calculate distance "along the line": Once for attacker, and then for each unit. The formula is x*b - y*a - that gives distance "along" the line - the real distance is then Abs(testedDist - attackerDist).

The traceline has height of BaseHeight - Tan(CameraPitch)*Distance. You know the distance now, so just test whether the unit's height is lower than this, and unit's height+1 (or some other constant) is higher. The constant (the 1) will determine the unit's collision height.
 
Level 1
Joined
Feb 1, 2009
Messages
5
#1.Thank you for the help, I have a question.. The link I provided, has SO much more code than the wiki you provided... They are both 2D. Yet you guys confirmed that both are the same system... I take it the one with less code is more efficient and easier to upgrade to 3D yes?

#2.Do these systems account for line of shot? Like cliffs, buildings... maybe even doodads?

#3.Also, lets say, I used this system to check for unit hits, and IF there are any unit hits, use the 100x loop system, to check for collision against terrain and doodads and such only, and only up to the distance of the targeted unit... So it wouldn't run more than needed.... (Since I am not looking for units, the 100x loop wouldn't be as "slow" right? But would give me the added functionality..

Please ignore the next line if it would change your mind on posting a response...
I'm rather intelligent... But math stopped becoming my strong point when advanced algebra and geometry came into my life, so all of this is rather confusing to me... So if you could please try to explain it in more detail that way I can learn it, I would be most grateful.
 
Last edited:
Level 12
Joined
Apr 15, 2008
Messages
1,063
#1 The version here on hive seems longer, but that's because it is in trigger format (IF-then-else takes 5 lines of "code"), while the version on wiki is pseudo-code.
Also, the entire second part (anything outside the "Pick units in unit group") is replaced in the wiki version with "Closest unit from unit group" function.

#2 No, it only accounts for units (Buildings are units). Cliffs would require some sort of looping check.

#3 Exactly, the biggest problem of the "100 loop" system are 100 "Unit in range" calls. If you call "Is point pathable" 100 times, there is not going to be a problem. (Although you hardly need 100 steps, assuming you have normal "thick" footprints, you can use quite big step)

Math:
Basically the system only uses a dot product (multiplication of according vector coordinates (X*X, Y*Y) and adding them together). The value is equal to |A|*|B|*cos(angle between A and B) -> if you divide it by |B| (or use B with length of 1), then you get |A|*cos, which you can use to calculate "length along a line". To calculate a unit's distance from the line of shot, use a vector perpendicular to the line of shot (vectors (A,B) and (-B,A) are perpendicular) to multiply the target unit's offset from attacker. The absolute value is distance, positivness shows the side on which the target is.
 
Status
Not open for further replies.
Top