Spawing at a relative distance

This forum is currently in read-only mode.
From the Asset Store
This sound library contains 197 rifle sounds. The sounds of gunshots, the sounds of shots from a distance
  • I'm starting to live up to my name.

    Here is another random question:

    Is it possible to spawn an object at a distance away relative to other instances of that same object that are already on-scream/in-layout?

    EX: There are 2 or 3 instance of object "Baddie" (for lack of a better name) and I want to spawn another one during runtime, but it has to be at least "x" number of pixels away from ALL instance of "Baddie".

  • To keep a distance, you'd need at least an angle. If you have nothing, you have to play it safe. One way to do the safe way is this:

    + [creation trigger]

    ++ For each baddie

    +++ baddie.x < global('lowX')

    ---> set global 'lowX' to baddie.x

    +++ baddie.x > global('hiX')

    ---> set global 'hiX' to baddie.x

    +++ baddie.y < global('lowY')

    ---> set global 'lowY' to baddie.y

    +++ baddie.y > global('hiY')

    ---> set global 'hiY' to baddie.y

    ++ [empty]

    --> Create baddie at

    (lowX + (hiX - lowX) / 2) + cos([your angle of choice]) * (distance(lowX, lowY, hiX, hiY) / 2 + [your distance of choice])

    (lowY + (hiY - lowY) / 2) + sin([your angle of choice]) * (distance(lowX, lowY, hiX, hiY) / 2 + [your distance of choice])

    Here's a picture describing the code above:

    <img src="http://dl.dropbox.com/u/11182740/pictures/get_distance.png" border="0" />

    By calculating the lowest and highest values for X and Y of the sprites (the blck dots) you get two new coordinates (the crosses) that are the outer bounds enclosing all sprites. If you now half the distance between them you have the radius of a circle with a center point between the two coords (red line = radius, red dot = center point, orange line = distance between coords). With the center point, the radius, any angle and your desired minimum distance you can now set a new spawn point.

  • Thank you for the reply.

    This isn't exactly what I am looking for however(although extremely useful), and that is my fault for not explaining properly

    If I were to use your image as an example, I am trying to spawn "Baddie" within and around the "radius"(within the playfield or where ever). Spawning them at distance "x" relative to EACH individual "baddie" rather than all of the objects as a whole or group.

    I don't know, may something like:

    +If random(XY) of playfield is not overlapping "baddie".XY + global(radius/distance)

    --> Spawn "baddie" at position XY

    This isn't the best example and I apologize if I am not making any sense.

  • I see. So you want to spawn it on some random spot and just make sure it is a minimum distance away from all other ones? Than your proposal is the easiest one. You should go for it.

  • +Every 100 ticks

    --> global variable 'TempX' = random (playfeild.width)

        global variable 'TempY' = random (playfeild.height)

    ++System: (inverted) /= "Baddie" at TempX+cos(360)*distance, TempY+sin(360)*distance

    --->System: create "Baddie" at TempX,TempY

    I think this is right, but I am still getting overlap.

    I think its because of the angle being 360. Maybe its only checking that one angle at the distance chosen relative to "Baddie" instead of the entire radius.

    Would I need to do a loop that checks the object 360 degrees around? That would be completely inefficient though. That 360 checks * the number of objects. <img src="smileys/smiley5.gif" border="0" align="middle" />

    Do I also need a for each "baddie"?

  • Thanks. I got it to work using the LOS behavior.

    +If "Baddie" has LOS on TempX, TempY

    -->blank

    +else

    -->System: Create "Baddie" at TempX, TempY

    For some reason it doesn't work with the condition inverted.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Nice solution you found here.

    sin() and cos() work on a specific angle, not an angle range. That's what I meant in my first post. The line of sight behavior also needs an angle but calculates it from the positions of Baddie and TempX/Y, so you don't need to calculate it yourself.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)