Random Motion

Hi
I need a random move of more or less 20 objects.
My idea is something like this :
First I am drawing objects and then in the enterFrame function :

  
for i = 1, #dots do  
  
if (dots[i].x % 2 == 0 ) then  
 dots[i].x = dots[i].x + 2  
 else  
 dots[i].x = dots[i].x - 2  
end  
end  

The same I am doing for y .
I understand that is not perfectly good.
Do you have any better idea for random movement… ? , maybe is someone good in JavaScript :
This is very good site with scripts…

http://www.motionscript.com/mastering-expressions/random-1.html

random motion - more chaotic will be just perfect.
[import]uid: 13156 topic_id: 18310 reply_id: 318310[/import]

maybe something like this ?

local takeRandom = function(min, max)  
 return math.floor(math.random(min, max))  
end  
  
for i = 1, #dots do  
local move  
local forward  
move = function()  
 forward = function()  
 dotsMove = transition.to(dots[i], {time = 8000, x = takeRandom(10, 450), y = takeRandom(10,300), onComplete = move})  
 end  
 dotsMove = transition.to(dots[i], {time = 8000, x = takeRandom(10, 450), y = takeRandom(10,300), onComplete = forward})  
 end   
move()  
  
end  

This is without enterFrame function of course .
But this will iterate #dots table move and forward and thats it. If I make a loop with no end how do I stop it manually ?
[import]uid: 13156 topic_id: 18310 reply_id: 70175[/import]

If you were doing a for loop and wanted an easy way to switch it off when something happened you could use a flag, if the flag was X run with it, if it was Y return false.

Peach :slight_smile: [import]uid: 52491 topic_id: 18310 reply_id: 70285[/import]

Peach Thank you for trying help.
Right now loop is going only once and moving all objects in different places each time. Cool

by the way… making this kind of movement is better to use transition.to then in enterFrame function right ? It’s " lighter" ?
transition.to has enterFrame inside I guess . [import]uid: 13156 topic_id: 18310 reply_id: 70341[/import]

Hey Piotr,

For checking what is “lighter” under any circumstances you can use this code - very useful in my experience;

[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 18310 reply_id: 70349[/import]

Hi I have to came back to random move issue …

local takeRandom = function(min, max)  
 return math.floor(math.random(min, max))  
end  
   
for i = 1, #dots do  
local move  
local forward  
move = function()  
 forward = function()  
 dotsMove = transition.to(dots[i], {time = 8000, x = takeRandom(10, 450), y = takeRandom(10,300), onComplete = move})  
 end  
 dotsMove = transition.to(dots[i], {time = 8000, x = takeRandom(10, 450), y = takeRandom(10,300), onComplete = forward})  
 end   
move()  
   
end  

This one does’t look so good because all objects moving it different positions but they are change it in the same time.
Any other suggestions how to random move objects ?
[import]uid: 13156 topic_id: 18310 reply_id: 76996[/import]

I would think the overhead in your example using transitions and calling a move function would be far greater than just having an enterFrame handler.

Keep in mind the magic behind transition.to involves it being processed every frame. Your original idea of a simple for loop adjusting the X and Y involves less code. At a machine level just calling a function involves several machine instructions, a push to the stack for each passed parameter, a goto to enter the code base, popping the parmeters back off and pushing the return values on to the stack and final and that’s all without doing anything.

You’re still running your for loop, so you’re paying that price regardless.

Then a function like transition.to has a log of code to make it usable in multiple circumstances, and all of that code has to execute even if you’re not using it (testing what parameters are passed, etc.)
transition.to is great when you want to do several things at once like move and fade or apply easings. But if all you’re doing is changing the X and Y, your better off doing it yourself. [import]uid: 19626 topic_id: 18310 reply_id: 77006[/import]

robmiracle thank you for very good explanation of this .
The problem is that I don’t know exactly how to make move random in then enter frame.
I have no idea how to adjust x and ( or ) y in way to make move random. I can adjust only plus or minus because I wand to keep same speed of move. It’s not so easy.

In transition.to I just giving to my objects end point ant thats it.
I’ve found a lot of examples of random move but in action script and there the enterFrame works a little bit different I guess.
[import]uid: 13156 topic_id: 18310 reply_id: 77008[/import]

I do something like this:

--   
-- when you create the dots, add a variable .direction and set it to 1  
-- i.e. dots[i].direction = 1  
--  
  
for i = 1, #dots do  
 if(math.random(100 \* FPS) \< 10) then  
 dots[i].direction = dots[i].direction \* -1  
 end   
 dots[i].x = dots[i].x + (2 \* dots[i].direction)  
end  

In this case, I’ve given the dot a 10% chance of changing directions. FPS is whatever your game is set to. Ergo:
math.random(3000) for a 30fps app or math.random(6000) for a 60 fps app.

you can tweek the percent chance of changing direction as necessary.

One thing though, moving 2 pixels every frame is going to be pretty darn fast. On a 60fps app, your going to move 120 pixels a second.

You will need to figure out how many pixels per frame you really need to move and you may find its a fractional number like half a pixel (meaning it actually moves every other frame). Keep in mind that .x and .y can be floating point numbers,
[import]uid: 19626 topic_id: 18310 reply_id: 77016[/import]

nice but it this case an angle of move for all dots will be the same…
[import]uid: 13156 topic_id: 18310 reply_id: 77027[/import]

It shouldn’t be. Each dot has its own direction. You may need two .direction’s. like:

dots[i].directionX
dots[i].directionY

so that you can deal with both. [import]uid: 19626 topic_id: 18310 reply_id: 77031[/import]