Spawning objects around a central point

Get help using Construct 2

Post » Thu Jan 31, 2013 8:53 pm

Hello all,

This is my first post so nice to meet you

I'm looking to spawn objects in a circle x distance away from the central point of the screen (x:windowWidth/2, y:windowHeight/2).

The angle being random(360).

I had this working in normal javascript like this:

enemy.x = (Math.sin(randomAngle * TO_RADIANS) * clearance) + (canvas.width/2) - (enemy.width/2);

enemy.y = (Math.cos(randomAngle * TO_RADIANS) * clearance) + (canvas.height/2) - (enemy.height/2);

Where clearance was the length of the centre point to the corner + some extra pixels to make sure they spawn off screen.

I just can't seem to replicate this in Construct 2 when adding this to Create Object event:

X: sin(random(360))*((sqrt((windowWidth*windowWidth)+(windowHeight*windowHeight))/2)+100)+(windowWidth/2)


Could anyone help me out with this? I noticed the asteroid tutorial just spawns random width and then choose() between a few offscreen Y values.


Posts: 39
Reputation: 678

Post » Fri Feb 01, 2013 5:18 am

Ok I found the answer. Posting in case useful for anyone:

The 'should of been obvious' answer was that I was using a random(360) for both x and y which of course would have been different!

So, here's the process:

1. Create a global variable 'randomAngle'
2. On the event of creating an object, set randomAngle to random(360)
3. Set X value of object to:


4. Set Y value of object to:


The +100 in the equations provides some extra clearance off screen, can be whatever you want.

If you created a 1000 objects and tested this it should draw a circle of them around the central point of your screen.

- The equation uses Pythagoras equation (h = x + y) to get the distance from top-left corner to bottom-right corner of the screen.

- Then halves it (/2) to get central point to corner of the screen.

- sin(angle) x hypot = opposite side (x) for X (s& = o/h)
- cos(angle) x hypot = adjacent side (y) for Y (c& = a/h)

The X & Y values at this point would be offset around the 0,0 coordinate so now it's a case of adding half the width and height to offset around the center of the screen.

Of course, you might be able to store much of these calculations at app start to reduce computations.

Hope that helps anyone having a similar issue :)
Posts: 39
Reputation: 678

Post » Fri Feb 01, 2013 6:19 am

Wow ) Many formulas )) Can you add some capx, for better understanding your problem and its solution?SysDiman2013-02-01 06:20:07
Posts: 25
Reputation: 4,598

Post » Fri Feb 01, 2013 6:43 am

Here you go:

I've put -200 in the formulas to get the spawn point on screen. Of course you can manipulate this to do any circular spawning around a point. This is just one application of it.
Posts: 39
Reputation: 678

Post » Fri Feb 01, 2013 7:45 am

If you don't really need a radius you can just use spawnPoint.x + random(-10,10), same for y.
Image Image
Posts: 7,358
Reputation: 67,271

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 23 guests