Spawn objects on different coordinates

Hello,

i have a little trouble with spawning objects on a different coordinates, if i just use math.random for determine their positions, object will overlap each other at some point

how can i not let this happen, so every object will be spawned with at least some distance between then?

any help is appreciated

There are many attempts
You could spawn those objects as physical objects with isSensor, give them a collision handler and when colliding, removing.
I don't know if those objects would actually flash..

Another method would be to make random coordinates and then loop through every object and check if it's at least some pixels away.

Here's some pseudoCode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
llocal mySpawnedObjects = {}
 
local function distance(ax,ay,bx,by)
        return (math.sqrt(math.pow(bx-ax,2)+math.pow(by-ay,2)))
end
 
local function spawnObject()
        local rX = math.random(0,display.contentWidth)
        local rY = math.random(0,display.contentHeight)
 
        for i=0,#mySpawnedObjects,1 do
                if (#mySpawnedObjects == 0) then
                        -- First Object, spawn anywhere
                        local myObject = display.newRect(rX,rY,10,10)
                        table.insert(mySpawnedObjects,myObject)
                else
                        -- Min Distance - 100 pixels
                        -- we need to do i+1 since lua tables start with the index of 1
                        local distance = math.round(distance(rX,rY,mySpawnedObjects[i+1].x,mySpawnedObjects[i+1].y))
                        print(distance)
                        if (distance >= 100) then
                                -- create Object and place it into the array
                                local myObject = display.newRect(rX,rY,10,10)
                                table.insert(mySpawnedObjects,myObject)
                        else
                                print("too close, can't spawn")
                        end
                end
        end
end
 
local spawnTimer = timer.performWithDelay(500,spawnObject,0)
<code>
 
Can't test it right now, but will test it later. 

sorry, objects still overlapping with your code

Had a stupid mistake ;)
Try this one!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local mySpawnedObjects = {}
 
local function distance(ax,ay,bx,by)
        return (math.sqrt(math.pow((bx-ax),2)+math.pow((by-ay),2)))
end
 
--local maxObjects = 20
 
local function spawnObject()
        local rX = math.random(0,display.contentWidth)
        local rY = math.random(0,display.contentHeight)
        local isFarEnough = true
        print("===============================")
        if (#mySpawnedObjects == 0) then
                -- First Object, spawn anywhere
                local myObject = display.newCircle(rX,rY,20)
                table.insert(mySpawnedObjects,myObject)
        else
                for i=#mySpawnedObjects,1,-1 do
                        -- Min Distance - 100 pixels
                        -- we need to do i+1 since lua tables start with the index of 1
                        local distance = math.round(distance(rX,rY,mySpawnedObjects[i].x,mySpawnedObjects[i].y))
                        print("=    " ..distance .."       =")
                        if (distance < 40) then
                                isFarEnough = false
                        end
                end
                if (isFarEnough) then
                        -- create Object and place it into the array
                        local myObject = display.newCircle(rX,rY,20)
                        table.insert(mySpawnedObjects,myObject)
                end
        end
end
 
local spawnTimer = timer.performWithDelay(100,spawnObject,0)

it works perfectly, thanks xxxfanta, you're great

You're welcome! :)

i have a trouble with your code
when i'm trying to remove objects and then spawn them again i get this error:
attempt to perform arithmetic on local "bx" (a nil value)

what's i'm supposed to do? is there a best way to remove objects so this problem would not occur?

Make sure to cancel the timer first!

timer:cancel(spawnTimer)

And don't forget removing objects "reversed"
i.e.:

1
2
3
4
for i=#mySpawnedObjects,1,-1 do
        mySpawnedObjects[i]:removeSelf();
        mySpawnedObjects[i] = nil;
end

glad for help, but alrady solved my problems)

by the way, why use reversed loop? is there any practical difference to simple loop?

Sure.
When you remove the objects from 1 to #mySpawnedObjects, you'll get an error at half the way.
Why? Because those indices don't exist anymore!

If you have a table with t[1], t[2], ... t[5] and remove the first one, everyone will move in their position (since LUA tables don't want to have "holes").
Therefor after deleting 3 of those, he can't find t[4], since there are only t[1] and t[2] left.

Hope that makes sense

that makes sense, thanks
though for removing all things at once i find its ok to use simple loop

Well you still should use a reversed one, otherwise you will run into errors! (since you can't delete everything at once)

views:1505 update:2011/11/19 17:31:56
corona forums © 2003-2011