Spawning objects around a central point

0 favourites
  • 5 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • Hello all,

    This is my first post so nice to meet you <img src="smileys/smiley1.gif" border="0" align="middle" />

    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)

    Y:cos(random(360))*((sqrt((windowWidth*windowWidth)+(windowHeight*windowHeight))/2)+100)+(windowHeight/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.

    Thanks

  • 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:

    (sin(randomAngle)*((sqrt((windowWidth*windowWidth)+(windowHeight*windowHeight))/2)+100))+(windowWidth/2)

    4. Set Y value of object to:

    (cos(randomAngle)*((sqrt((windowWidth*windowWidth)+(windowHeight*windowHeight))/2)+100))+(windowHeight/2)

    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 :)

  • Wow ) Many formulas )) Can you add some capx, for better understanding your problem and its solution?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here you go:

    dropbox.com/s/zvfs608yj6uizoo/circleSpawn.capx

    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.

  • If you don't really need a radius you can just use spawnPoint.x + random(-10,10), same for y.

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