Hello guys, i have some problems with random floating objects around screen:
I have table with 20 elements and for each element of table chooses random spawn point and random speed on x and y axis based on coordinated of spawn point. Heres my class code
Asteroids = class()
function Asteroids:init(x)
-- you can accept and set parameters here
--my main table
self.asteroids = {}
--spawn points table
self.points = {vec2(0,0),vec2(0,HEIGHT/2),vec2(0,HEIGHT),vec2(WIDTH/2,0),vec2(WIDTH/2,HEIGHT),vec2(WIDTH,0),vec2(WIDTH,HEIGHT/2),vec2(WIDTH,HEIGHT)}
-- print(#self.points)
self:insert()
end
function Asteroids:draw()
-- Codea does not automatically call this method
--just a timer
local time = math.floor(zero*10)
--iterate through table
for k,v in pairs(self.asteroids) do
--make my asteroids float
v["start"].x = v["start"].x + v["speed"].x
v["start"].y = v["start"].y + v["speed"].y
--just chooses one of two random pictures of asteroids
if v["size"] == 1 then
sprite("Space Art:Asteroid Small",v["start"].x,v["start"].y)
else
sprite("Space Art:Asteroid Large",v["start"].x,v["start"].y,140/2,116/2)
end
-- oob = Out Of Bounds
if v["start"].x > WIDTH+40 or v["start"].x < -40 or v["start"].y > HEIGHT+40 or v["start"].y < -40 then
v["oob"] = true
end
if v["oob"] == true then
end
end
-- print(#self.asteroids)
end
--now is the funny part
--heres code , which insert table with values of asteroid (coordinates, speed and etc)
--dont be scary about self.asteroids[i]["start"] i just use named values on table so this is how i call it in loop
--in this loop i fill my self.asteroids with values i described before and check where is spawn point and calculate random speed from this point
--but on screenshots below you can see what one sprite cover another
--and i want to avoid this overlaying
--i found, what numbers with float dot is very near to what i want, but first problem of this method is what sprite will go very slow and second is what it still covers another asteroid
function Asteroids:insert()
for i = 1,20 do
table.insert(self.asteroids,{start = self.points[math.random(#self.points)],oob = false,speed = vec2(0,0),size = 1})
if self.asteroids[i]["start"] == vec2(0,0) or self.asteroids[i]["start"] == vec2(0,HEIGHT/2) or self.asteroids[i]["start"] == vec2(WIDTH/2,0) then
self.asteroids[i]["speed"].x = math.random(1,3)
self.asteroids[i]["speed"].y = math.random(1,3)
elseif self.asteroids[i]["start"] == vec2(0,HEIGHT) or self.asteroids[i]["start"] == vec2(WIDTH/2,HEIGHT) then
self.asteroids[i]["speed"].x = math.random(1,3)
self.asteroids[i]["speed"].y = math.random(-3,-1)
elseif self.asteroids[i]["start"] == vec2(WIDTH,0) or self.asteroids[i]["start"] == vec2(WIDTH/2,0) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT/2) then
self.asteroids[i]["speed"].x = math.random(-3,-1)
self.asteroids[i]["speed"].y = math.random(1,3)
elseif self.asteroids[i]["start"] == vec2(WIDTH/2,HEIGHT) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT/2) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT) then
self.asteroids[i]["speed"].x = math.random(-3,-1)
self.asteroids[i]["speed"].y = math.random(-3,-1)
end
if math.random(0,1) == 1 then
self.asteroids[i]["size"] = 1
else
self.asteroids[i]["size"] = 0
end
-- print(self.asteroids[i]["speed"])
end
end
function Asteroids:touched(touch)
-- Codea does not automatically call this method
end