Spawing at a relative distance

For questions about using Classic.

Post » Wed Jul 25, 2012 3:19 am

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".
B
3
Posts: 59
Reputation: 418

Post » Wed Jul 25, 2012 7:01 am

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:


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.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Jul 25, 2012 9:10 pm

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.
B
3
Posts: 59
Reputation: 418

Post » Wed Jul 25, 2012 9:55 pm

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.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Jul 25, 2012 11:55 pm

+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.

Do I also need a for each "baddie"?
B
3
Posts: 59
Reputation: 418

Post » Thu Jul 26, 2012 1:25 am

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.RandomDood2012-07-26 01:27:40
B
3
Posts: 59
Reputation: 418

Post » Thu Jul 26, 2012 2:06 pm

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.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests