System Arrow System

Reaction score
86
Arrow System v 1.00
Requires Timer32(Make the "next" and "prev" members of T32x module not private)


Ver History:

11/23/2009: Ver 1.00 Released
Description: vJass System created for the simple purpose of making it extremely easy to create "projectiles" that start in one place and end up in another. This system DOES obey the laws of physics (neglecting air friction)and the constant of gravity can be changed (not in game). The main reason I created this system was simply because I started making it for my Archer map and decided that it was pretty useful so why not post it.

One major thing that I haven't completely finished yet was creating more BJ's. When I made this I envisioned a system that would give you functions to cope with almost any possibility.
For example, hit a certain Z before reaching target.
Reach the target in a certain amount of time.
And a lot more, which I haven't done yet or had the need to do.
(The only reason I haven't done so was because the need for it hasn't come up in the map. I will be making more functions soon.)

Usage:
Pretty simple:
JASS:
local Arrow A = ArrowHeightToXY(GetTriggerUnit(),0,0,10,1000,1000,500)
set A.HitGround = DestroyUnit
set A.range=100
set A.UnitInRange=DamageUnit
set A.ArrowInRange= DestroyArrow

function DestroyUnit takes Arrow A returns nothing
     call RemoveUnit(A.u)
endfunction
function DamageUnit takes Arrow A,unit targ returns nothing
     call UnitDamageTarget(A.u,targ,10....)
endfunction
function DestroyArrow takes Arrow A, Arrow B returns nothing
     call B.destroy()
endfunction

Creates an Arrow that fires the TriggeringUnit from (0,0) to (1000,1000) that peaks at a height of 500. If a unit enters within range of 100, it damages it. If an arrow enters within range of 100, it destroys it. When it hits the ground it removes the unit.

Members:
JASS:
struct Arrow
        unit u =null //Arrow unit
        real x =0 //X of arrow
        real y=0 //Y of arrow
        real z=0 //Z of arrow
        real vx=0 // X Velocity
        real vy=0 // Y Velocity
        real vz=0 // Z Velocity
        real time = 0 // Time passed since creation
        integer data=0 // integer data that can be stored
        real range=0 // Range in which a unit/arrow must enter for it to trigger call back. Leave as 0 if you don't want anything to happen when unit/arrow approaches.
        CallBacks HitGround=-1//When the Arrow hits the ground, calls this function. 
        CallBacks Period=-1 //Every Period it calls this function
        CallBacks Peak=-1 //When the Arrow is at it's peak it calls this function
        CallBacksArrow ArrowInRange=-1 //When arrow is in range it calls this function
        CallBacksUnit UnitInRange=-1 //When unit is in range, calls this function
...
endstruct


Code:
JASS:
library ArrowSys requires T32
    globals
        constant real Arrow_g = .981
    endglobals
    private function SetUnitZ takes unit u, real z returns nothing
        call UnitAddAbility(u,'Amrf')
        call SetUnitFlyHeight(u,z,0)
        call UnitRemoveAbility(u,'Amrf')
    endfunction
    function ArrowTimedToXY takes unit u, real fX, real fY, real fZ, real toX, real toY, real time returns Arrow
        local Arrow A = Arrow.create(u)
        local real dx=toX-fX
        local real dy=toY-fY
        local real theta = Atan2(dy,dx)
        local real dist = SquareRoot(dx*dx+dy*dy)
        local real gV
        set A.x=fX
        set A.y=fY
        set A.z=fZ
        set gV= dist/(time)
        set A.vx= gV*Cos(theta)
        set A.vy=gV*Sin(theta)
        set A.vz=Arrow_g*time/2
        return A
    endfunction
    function ArrowHeightToXY takes unit u, real fX, real fY, real fZ, real toX, real toY, real height returns Arrow
        local Arrow A = Arrow.create(u)
        local real dx=toX-fX
        local real dy=toY-fY
        local real theta = Atan2(dy,dx)
        local real dist = SquareRoot(dx*dx+dy*dy)
        local real gV
        set A.x=fX
        set A.y=fY
        set A.z=fZ
        set A.vz=SquareRoot(2*Arrow_g*height)
        set gV= dist/(2*A.vz/Arrow_g)
        set A.vx= gV*Cos(theta)
        set A.vy=gV*Sin(theta)
        return A
    endfunction
    function interface CallBacks takes Arrow A returns nothing
    function interface CallBacksArrow takes Arrow A, Arrow B returns nothing
    function interface CallBacksUnit takes Arrow A, unit B returns nothing
    private function True takes nothing returns boolean
        return true
    endfunction
    struct Arrow
        unit u =null
        real x =0
        real y=0
        real z=0
        real vx=0
        real vy=0
        real vz=0
        real time = 0
        integer data=0
        real range=0
        CallBacks HitGround=-1
        CallBacks Period=-1
        CallBacks Peak=-1
        CallBacksArrow ArrowInRange=-1
        CallBacksUnit UnitInRange=-1
        static method create takes unit u returns Arrow
            local Arrow X = Arrow.allocate()
            call X.startPeriodic()
            set X.u=u
            return X
        endmethod
        static method GetArrowsInRange takes Arrow A, real range returns nothing
            local Arrow cur = Arrow(0).next
            local real x
            local real y
            local real z
            loop
            exitwhen cur==0
                set x = cur.x-A.x
                set y = cur.y-A.y
                set z= cur.z-A.z
                if(A!=cur and SquareRoot(x*x+y*y+z*z)<range)then
                    call A.ArrowInRange.evaluate(A, cur)
                endif
                set cur=cur.next
            endloop
        endmethod
        static method GetUnitsInRange takes Arrow A, real range returns nothing
            local Arrow cur = Arrow(0).next
            local group g = CreateGroup()
            local real x
            local real y
            local real z
            local unit u
            call GroupEnumUnitsInRange(g,A.x,A.y,range+100,Condition(function True))
            loop
            set u = FirstOfGroup(g)
            exitwhen u==null
                set x = GetUnitX(u)-A.x
                set y = GetUnitY(u)-A.y
                set z= GetUnitFlyHeight(u)-A.z
                if(A.u!=u and SquareRoot(x*x+y*y+z*z)<range)then
                    call A.UnitInRange.evaluate(A,u)
                endif
                call GroupRemoveUnit(g,u)
            endloop
            call DestroyGroup(g)
            set g=null
        endmethod
        method onDestroy takes nothing returns nothing
            call .stopPeriodic()
        endmethod
        method periodic takes nothing returns boolean
            local boolean pos= .vz>0
            set .x = .x+.vx
            set .y=.y+.vy
            set .z=.z+.vz
            set .vz = .vz-Arrow_g
            if(Period !=-1)then
                call Period.evaluate(this)
            endif
            if(range!=0)then
                call Arrow.GetArrowsInRange(this,range)
                call Arrow.GetUnitsInRange(this,range)
            endif
            if(pos and .vz<0 and Peak!=-1)then
                call Peak.evaluate(this)
            endif
            call SetUnitX(.u,.x)
            call SetUnitY(.u,.y)
            call SetUnitZ(.u,.z)
            if(.z<0)then
                call HitGround.evaluate(this)
                call .destroy()
                return true
            endif
            set time=time+T32_PERIOD
            return false
        endmethod
        implement T32x
    endstruct
endlibrary
 
Reaction score
86
@Executor
KK, I'll change it in a bit. Ill change it when I get back.

@Jesus4Lyf
lol. Yeah. I was thinking whats the point of creating two more variables if I could use T32's.
 

Kenny

Back for now.
Reaction score
202
You serious?!?! Hahaha. I was thinking about releasing my projectile system pretty soon.

I guess a test map would be nice. :)
 

Kenny

Back for now.
Reaction score
202
I'm not sure if your GetUnitsInRange() function works all to well.

A units fly height may be 0.00 but its model height could be 150.00.

If you have an arrow with a radius of 100.00, it would be cutting through the units body before detecting that it is within range.

Also, you don't group up already seen units, meaning your UnitsInRange function could fire multiple times while passing a unit.

It looks pretty ugly.

You should be using T32_Tick as well.

And get rid of the dynamic group in your GetUnitsInRange() function.
 

game_war48

New Member
Reaction score
5
JassHelper "find" an error:
JASS:
            local Arrow cur = Arrow(0).next

"next is not a member of Arrow"
I'm using JassHelper ver 0.A.2.7

EDIT: err... I was fix it. Don't need to answer.
 

Nestharus

o-o
Reaction score
84
Edit
rofl, this actually looks like an essay, intro, 3 paragraphs, and a conclusion, haha : D.
End Edit

Introduction-
If you want to do a resource like this really well, a different kind of design is required.

Not really going to go through the code because I've already gone through like 10 of these and there's only one way to program the different aspects of a system like this, so the answer's always the same : |.

Also this is a terrible name. When I saw this, I thought Arrow Key System.

#1-
Simple Projectiles (just from one spot to another)

Homing Projectiles

Simple Physics Projectiles (one spot to another)

Homing Physics Projects

Trajectory via algorithms and direct coordinates relative or not relative to a given origin via algorithms or direct coordinates (Spawn actually did this)


#2
Now, as for the physics, you have simple physics and advanced physics. In advanced physics, every object has its own gravitational force (just like the real world). With this you can create gravity wells, orbiting objects, and just do a whole mess of cool things. Advanced also has gravitation force for different areas on the map.

Simple is one global constant (like what you have).


You also need to do projectile paths via algorithms and direct coordinates.

#3
These are just my personal opinions : ). Now, I don't know of any projectile system that's done this yet, so this is actually on my to-do list, lol. I've already helped too many people with their systems and they've all done around the same things ><. There's only one way to code the various aspects of a projectile system (simple, homing, etc), so it's not like all of these systems are needed : |.

Now, I already have a snippet that is literally as optimal as possible for homing and non-homing projectiles, but I haven't released it yet because of the various features above that I believe are also necessary in a projectile system.


Again, these are just my own thoughts on the matter.

-Conclusion-
So, I personally think the design is all wrong and I don't think anyone's done this right yet ;o. I've only seen a few systems that i believed were done 100% right, and jesus4lyf is one of the primary producers of these systems, lol : D, so I actually have a lot of respect for him ^_^.

Really, coming up with simple designs to address these issues can be very challenging, so lately I've been going down a different path to give maximum ease of use and maximum performance.
 
Reaction score
86
@kenny!: fixed those in a newer version, haven't posted yet.

@Nestharus
Okay, the name is a bit misleading.
#1 I dont know what point your trying to make.
#2 It's very simple to fix something like that.
#3 is that a point?

thanks for the crit.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top