Spawn random objects near a existing object?

Hi everyone,

I followed a tutorial recently on how to spawn objects randomly, but is seems to me they calculate the spawnpoint from the worlds 0 point. Now i want them to spawn near an object, so from the local position of this object. So i would add a collisionsensor to an object which sends a message to an empty that there should spawn an object within some limited distances near that object again.

Here’s tyhe tutorial i followed https://www.youtube.com/watch?v=126i_aO1OoE

I have this code so far:

Code:
import bge
import random
scene = bge.logic.getCurrentScene()
Spawnpoint = scene.objects [“Spawnpoint”]

class PlatformspawnClass:
def init(self, x, y, z):
self.x = x
self.y = y
self.z = z
Spawnpoint.position = (x,y,z)
scene.addObject(“Platformspawn”, Spawnpoint, 100)

xPosition = random.randint(-3,3)
yPosition = random.randint(1,3)
zPosition = random.randint(1,3)

object1 = PlatformspawnClass(xPosition, yPosition, zPosition)

And this works pretty fine so far, they spawn near the origin point of the world, what can i change in the code to make them spawn near my object where this script is attatched to?

Thanks in advance :wink:

This is the the snippet, now it works for a flat plane, you can add a third dimension as well if you want.


import math
import random


def spawn_coords_quad(x, y, distx, disty):
    randx = (random.random() * distx) - (distx * 0.5);
    randy = (random.random() * disty) - (distx * 0.5);
    return [x + randx, y + randy]


print(spawn_coords_quad(10, 10, 1, 1))

I solved it :slight_smile: by combining several tips and tricks :smiley: