creating random torque and applied force question

I am hoping someone with a better understanding of the physics engin can give me a suggestion on my code. I have a game in Flash that I am porting to Corona. Basically an Astroids clone. I can’t seem to get the random math correct to apply the correct force and torque to the astroids.

All the astroids seem to only move from left to right on the screen. The speed does very. It’s more about the direction that I am trying to randomize.

Here is the code.

[lua]local function createAstroids( )

for i=1, numberOfAstroidsForThisLevel() do
astroid = display.newImage(“rock_large.png”)
astroid.x = (math.random() * ( 0- display.contentWidth)) + display.contentWidth
astroid.y = 0 - (astroid.height - 2)

astroid_group:insert(astroid)

physics.addBody(astroid, {density=3})

xforce = (math.random() * (0 - display.contentWidth)) + (display.contentWidth * 2)
yforce = ( math.random() * (0 - display.contentHeight)) + (display.contentHeight * 2)
atorque = (math.random() * -1000) + 1000

xposition = (math.random() * (0 - astroid.x)) + (astroid.x * 2)
yposition = (math.random() * (0 - astroid.y)) + (astroid.y * 2)

astroid:applyForce(xforce,yforce, xposition, yposition)
astroid:applyTorque(atorque)
end
end[/lua]

Here is the Flash version so that you get a sense of what I am going for. http://matthewsloanwallace.com/flixel-progress-flying-space-rocks-part-6
[import]uid: 6636 topic_id: 3280 reply_id: 303280[/import]

something like below will pick a random angle, and with a set force work out the x and y components of the force

I think you would need to do an impulse rather than a force but it probably doesnt matter

local angle = math.random( 0 , 360 ) local sin = math.sin local cos = math.cos local pi = math.pi local force = 0.7 local xFinal = ( sin( angle \* pi / 180 ) \* force ) local yFinal = ( cos( angle \* pi / 180 ) \* force ) YOUROBJECT:applyForce( xFinal , -yFinal, YOUROBJECT.x, YOUROBJECT.y ) [import]uid: 5354 topic_id: 3280 reply_id: 9865[/import]