Find nearest enemy

pixlepixpixlepix Member Posts: 12
edited December 2011 in Working with GS (Mac)
So, I have a tower-like thing. How do I find the nearesy enemy to fire at.

Comments

  • POMPOM Member Posts: 2,599
    edited December 2011
    To find distance between actors you need to use the "Magnitude" expression ,
    The way i did it in my game was instead making the good guy know the distance from all the enemies , i made the enemies find the distance from a single good guy ..
    So this is what i did ,
    The hero- X and Y values are stored to 2 game.attributes via constrains :
    Game.Hero X position TO self.position.X
    Game.Hero Y position TO self.position.Y

    Then , in the enemies you make a self.attribute - integer - lets call it "distance from hero"
    In the enemies you make this : (you can use constrain but i prefer timer cause i learned that magnitude calculating is slowing GS to crawl)
    Timer - Every 0.5 sec
    Change "self.distance from hero" TO magnitude(self.position.X - "Game.Hero X position" , self.position.Y - "Game.Hero Y position")

    This way you get in each enemy the number of pixels between them and the hero , and you can do whatever you want with this number .

    Hope it help you.
    Roy.
  • pixlepixpixlepix Member Posts: 12
    edited December 2011
    Problem is, I have more then one tower. Got an idea though.
    edit: is there any way to have a list of varibles?
  • mu-kowAPPSmu-kowAPPS Member Posts: 233
    To find closest

    Create new real attribute

    Constrain your new attribute to min(tower1dist,tower2dist,tower3dist,tower4dist)

    Then you can add some rules to activate if it is the closest

    If shortestdist = tower2dist
    Move to tower2x tower2y

    Or whatever u want to do with it
  • pixlepixpixlepix Member Posts: 12
    Any way to do that with mpore then one tower though?
  • pixlepixpixlepix Member Posts: 12
    So, anyone know of a way to find the nearest enemy for multiple towers?
  • SlickZeroSlickZero Houston, TexasMember, Sous Chef Posts: 2,870
    If so many magnitudes slow down your game performance, you could just have invisible actors positioned around the tower, and when enemies come into contact with those actors, change an attribute that tells the tower to shoot towards their position.
  • mu-kowAPPSmu-kowAPPS Member Posts: 233
    I told u how to find nearest enemy, you constrain magnitude of all of your towers to real attributes and then constrain another attribut to min(tower1dist,tower2dist,tower3dist,ect...)

    min(x,y,z,etc...) returns the smallest number of the entries inside the parenthesis. Soooo. if x=10,y=35,z=377. min(x,y,z) would =10

    Get it?
  • BuruBuruBuruBuru Member Posts: 20
    Hey there,
    I am still a newbie in Gamesalad.
    From what I understand using magnitude slows your game, so instead of using this function would it be more efficient using a mathematical expression?
    The distance between two actor can calculated in this way: distance = sqr((x1-x2)^2 +(y1-y2)^2)
    Where x1 and y1 are the coordinates of the first actor and x2 and y2 are the coordinates of the sceond one.
    To obtain an integer value simply add a floor function to sqr.

    I haven't tried it yet... Could someone tell me if a mathematcal expression is better then a preset function in terms of performance?

    Cerulean Swan Games
    ________________________________________

    Visit our new Website at :Cerulean Swan Games
    Follow Us On Twitter and Like Us On Facebook!
  • mu-kowAPPSmu-kowAPPS Member Posts: 233
    I do not believe that magnitude is that heavy of a function. Constrain on the other hand is heavy.
  • POMPOM Member Posts: 2,599
    edited December 2011
    @mu-kowAPPS
    Although i appreciate what you think , i have made some intensive tests in most of the GameSalad behaviors and expressions to check how cpu heavy they are and my finding are clearly pointing that the "Magnitude" function is one of the most cpu heavy function in game salad , more then a Timer AND more than simple constrain .
    (i say simple constrain meaning constraining an attribute to a value - not to an expression)
    Off-course if you have doubts i can prove it , i never say something in the forum if I've not tested it before ,
    and if I'm not sure about something ... i say that I'm not sure about it :D

    I can tell you that :
    Combinig a constrain with magnitude like:
    Constrain "self.size.with
    Roy.
  • pixlepixpixlepix Member Posts: 12
    @mu-kowAPPS
    Unless I am missing something, that will only work with one tower. I need something that will work with unlimited towers.
  • mu-kowAPPSmu-kowAPPS Member Posts: 233
    @P-O-M i use constrain with magnitude in my game CP Tanks. performance is fine, at least on armv7 devices, i wont be making my app available for armv6. i have 17 actors that all constrain their magnitude to game attributes. it may be one of the more intensive math functions, but using it in 10-30 actors shouldnt destroy your games preformance (i am only speaking of armv7, because i have no armv6 to test on and cannot say for sure). spawning however is a crazy performance hog!

    @pixlepix each tower would have to constrain its magnitude to a game attribute and then you would have to do a min(x,y,z,...) where x,y,z... is each towers attribute. that will tell you which one is closest. then if you want something to happen depending on which one is closest, say....

    if game.shortest.dist = tower3dist
    play sound (fart.ogg)

    if game.shortestdist = tower7dist
    play sound (siren.ogg)

    if game.shortestdist = tower27dist
    play sound (sorrydaveicannotallowthat.ogg)

    im not sure how you would accomplish what you want for an infinite number of towers, you may just have to make attributes for as many towers can fit in your levels.

    in my game cp tanks, i have 3 light tank prototypes, 4 med tanks, 4 heavy tanks, 4 turrets, and 2 choppers, i had to make 6 game attributes for each. i just had to be creative and section some of the levels into waves, but thats not so bad, having 17 enemies on the screen at once would be a bit hectic for the player. this may not be an option for you, but you may have to think of a creative solution as i did.

    good luck.
Sign In or Register to comment.