how to make an object move around the screen randomly ?!

Hi Corona community, 

I want to know how can I make an object move around the screen randomly :

as a first try I wrote this line :

[lua]function moveCircle()
transition.to(circle1,{time=1000, x=math.random(80,680), y=math.random(30,580), onComplete=moveCircle})
transition.to(circle2,{time=1000, x=math.random(80,680), y=math.random(30,580)})
end

moveCircle()[/lua]

but the problem I realized with this is that the collision detection is not happening like it should, and I also made walls but the circles are getting through the walls !

so I’m looking for another way to get the circles moving instead of the way I used.

Without seeing your code where you’re setting up your physics objects, any collision filters and your collision handler it’s going to be hard to advise you.

Generally speaking you should not move physics objects using transitions because you’re moving the objects outside of physics while physics is trying to maintain control of them.

Hi Rob,

here’s the code :

[lua]local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

sc1_bg = display.newImageRect(“sc1_bg.png”, 512, 420)
sc1_bg.x = centerX; sc1_bg.y = centerY; sc1_bg.xScale = 1.108
screenGroup:insert(sc1_bg)

circle = display.newImageRect(“circle.png”, 64, 64)
circle.anchorY = 0; circle.anchorX = 0;
physics.addBody(circle, “dynamic”, {radius = 31})

circle2 = display.newImageRect(“circle.png”, 64, 64)
circle2.anchorY = 0; circle2.anchorX = 0; circle2.x = 420;
physics.addBody(circle2, “dynamic”, {radius = 31})

ceiling_lv1 = display.newRect(centerX, screenTop,568,5)
screenGroup:insert(ceiling_lv1)
physics.addBody(ceiling_lv1, “static”, {friction = 1})

left_lv1 = display.newRect(screenLeft, centerY, 5, 390)
physics.addBody(left_lv1, “static”)

Bottom_lv1 = display.newRect(centerX, screenBottom, 568, 5)
physics.addBody(Bottom_lv1, “static”)

moveCircle()
 

end

function moveCircle()

transition.to(circle,{time=1000, x=math.random(80,680), y=math.random(30,580), onComplete=moveCircle})

transition.to(circle2,{time=1000, x=math.random(80,680), y=math.random(30,580)})
end[/lua]

and if I can’t move physical objects with transition, what is the best way to do that ?

Hi @hammod-930,

The first and most important thing here is that you need to treat physics objects as centrally-anchored. Second, what Rob means is that you can move physics objects using transitions, but once a collision occurs, you need to stop/cancel the transition because, if you don’t, the transition will attempt to keep going, but the physics engine will be fighting against that process because it collided with something.

Hope this helps,

Brent

Hi Brent, 

after reading Rob’s comment your comment I did these changes :[lua]function scene:createScene(e)

local screenGroup = self.view

sc1_bg = display.newImageRect(“sc1_bg.png”, 512, 420)
sc1_bg.x = centerX; sc1_bg.y = centerY; sc1_bg.xScale = 1.108
screenGroup:insert(sc1_bg)

circle = display.newImageRect(“circle.png”, 64, 64)
circle.anchorY = 0; circle.anchorX = 0;
physics.addBody(circle, “dynamic”, {radius = 31})

circle2 = display.newImageRect(“circle.png”, 64, 64)
circle2.anchorY = 0; circle2.anchorX = 0; circle2.x = 420;
physics.addBody(circle2, “dynamic”, {radius = 31})

ceiling_lv1 = display.newRect(centerX, screenTop,568,5)
screenGroup:insert(ceiling_lv1)
ceiling_lv1.name = “wall”
physics.addBody(ceiling_lv1, “static”, {friction = 1})

left_lv1 = display.newRect(screenLeft, centerY, 5, 390)
left_lv1.name = “wall”
physics.addBody(left_lv1, “static”)

Bottom_lv1 = display.newRect(centerX, screenBottom, 568, 5)
Bottom_lv1.name = “wall”
physics.addBody(Bottom_lv1, “static”)

moveCircle()
moveCircle2()
 

end

function moveCircle()
tra1 = transition.to(circle,{time=900, x=math.random(80,510), y=math.random(10,285), onComplete=moveCircle})
end

function moveCircle2()
tra2 = transition.to(circle2,{time=900, x=math.random(80,510), y=math.random(10,285), onComplete=moveCircle2})
end

function circle1_collision(e)
if e.phase == “began” then
if (e.other.name == “wall”) then
transition.cancel(tra1)
timer.performWithDelay(2, moveCircle, 1)
end
end
end

function circle2_collision(e)
if e.phase == “began” then
if (e.other.name == “wall”) then
transition.cancel(tra2)
timer.performWithDelay(2, moveCircle2, 1)
end
end
end

function scene:enterScene(e)

circle:addEventListener(“collision”, circle1_collision)
circle2:addEventListener(“collision”, circle2_collision)
end[/lua]

now this seems to work but it feels like there’s something missed ?!

Hi @hammod-930,

This looks fine at a quick overview. The only thing I’d suggest is that you set the transitions as properties of the actual circles or something like that, so they’re easier to manage and you don’t get any global variable declarations in your project. For example, in cases like this, I find it easier to set transitions on objects like:

[lua]

circle1.trans = transition.to( … )

[/lua]

Brent

Without seeing your code where you’re setting up your physics objects, any collision filters and your collision handler it’s going to be hard to advise you.

Generally speaking you should not move physics objects using transitions because you’re moving the objects outside of physics while physics is trying to maintain control of them.

Hi Rob,

here’s the code :

[lua]local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

sc1_bg = display.newImageRect(“sc1_bg.png”, 512, 420)
sc1_bg.x = centerX; sc1_bg.y = centerY; sc1_bg.xScale = 1.108
screenGroup:insert(sc1_bg)

circle = display.newImageRect(“circle.png”, 64, 64)
circle.anchorY = 0; circle.anchorX = 0;
physics.addBody(circle, “dynamic”, {radius = 31})

circle2 = display.newImageRect(“circle.png”, 64, 64)
circle2.anchorY = 0; circle2.anchorX = 0; circle2.x = 420;
physics.addBody(circle2, “dynamic”, {radius = 31})

ceiling_lv1 = display.newRect(centerX, screenTop,568,5)
screenGroup:insert(ceiling_lv1)
physics.addBody(ceiling_lv1, “static”, {friction = 1})

left_lv1 = display.newRect(screenLeft, centerY, 5, 390)
physics.addBody(left_lv1, “static”)

Bottom_lv1 = display.newRect(centerX, screenBottom, 568, 5)
physics.addBody(Bottom_lv1, “static”)

moveCircle()
 

end

function moveCircle()

transition.to(circle,{time=1000, x=math.random(80,680), y=math.random(30,580), onComplete=moveCircle})

transition.to(circle2,{time=1000, x=math.random(80,680), y=math.random(30,580)})
end[/lua]

and if I can’t move physical objects with transition, what is the best way to do that ?

Hi @hammod-930,

The first and most important thing here is that you need to treat physics objects as centrally-anchored. Second, what Rob means is that you can move physics objects using transitions, but once a collision occurs, you need to stop/cancel the transition because, if you don’t, the transition will attempt to keep going, but the physics engine will be fighting against that process because it collided with something.

Hope this helps,

Brent

Hi Brent, 

after reading Rob’s comment your comment I did these changes :[lua]function scene:createScene(e)

local screenGroup = self.view

sc1_bg = display.newImageRect(“sc1_bg.png”, 512, 420)
sc1_bg.x = centerX; sc1_bg.y = centerY; sc1_bg.xScale = 1.108
screenGroup:insert(sc1_bg)

circle = display.newImageRect(“circle.png”, 64, 64)
circle.anchorY = 0; circle.anchorX = 0;
physics.addBody(circle, “dynamic”, {radius = 31})

circle2 = display.newImageRect(“circle.png”, 64, 64)
circle2.anchorY = 0; circle2.anchorX = 0; circle2.x = 420;
physics.addBody(circle2, “dynamic”, {radius = 31})

ceiling_lv1 = display.newRect(centerX, screenTop,568,5)
screenGroup:insert(ceiling_lv1)
ceiling_lv1.name = “wall”
physics.addBody(ceiling_lv1, “static”, {friction = 1})

left_lv1 = display.newRect(screenLeft, centerY, 5, 390)
left_lv1.name = “wall”
physics.addBody(left_lv1, “static”)

Bottom_lv1 = display.newRect(centerX, screenBottom, 568, 5)
Bottom_lv1.name = “wall”
physics.addBody(Bottom_lv1, “static”)

moveCircle()
moveCircle2()
 

end

function moveCircle()
tra1 = transition.to(circle,{time=900, x=math.random(80,510), y=math.random(10,285), onComplete=moveCircle})
end

function moveCircle2()
tra2 = transition.to(circle2,{time=900, x=math.random(80,510), y=math.random(10,285), onComplete=moveCircle2})
end

function circle1_collision(e)
if e.phase == “began” then
if (e.other.name == “wall”) then
transition.cancel(tra1)
timer.performWithDelay(2, moveCircle, 1)
end
end
end

function circle2_collision(e)
if e.phase == “began” then
if (e.other.name == “wall”) then
transition.cancel(tra2)
timer.performWithDelay(2, moveCircle2, 1)
end
end
end

function scene:enterScene(e)

circle:addEventListener(“collision”, circle1_collision)
circle2:addEventListener(“collision”, circle2_collision)
end[/lua]

now this seems to work but it feels like there’s something missed ?!

Hi @hammod-930,

This looks fine at a quick overview. The only thing I’d suggest is that you set the transitions as properties of the actual circles or something like that, so they’re easier to manage and you don’t get any global variable declarations in your project. For example, in cases like this, I find it easier to set transitions on objects like:

[lua]

circle1.trans = transition.to( … )

[/lua]

Brent