Help with getting a constant velocity with random forces

Hello. I am making a game where a dynamic object randomly moves around the screen. My current code makes sure the two forces add up to a certain value. However this does not give the same velocity. Would there be any way to do that?

Here is my code currently

local impulseVal = 400 local impulseX local impulseY local signDet1 local signDet2local mRand = math.random math.randomseed( os.time() ) local function bubbleClick() --remove later bubble:setFillColor(55,10,0 ) print("rand") --get random number impulseX = mRand(0,impulseVal) impulseY = impulseVal - impulseX signDet1 = mRand(0,1) if signDet1 == 0 then impulseX = impulseX \* 1 elseif signDet1 == 1 then impulseX = impulseX \* -1 end signDet2 = mRand(0,1) if signDet2 == 0 then impulseY = impulseY \* 1 elseif signDet1 == 1 then impulseY = impulseY \* -1 end bubble:applyForce(impulseX,impulseY,bubble.x,bubble.y) end

I cannot use transition to because that messes up my object’s x and y coordinates.

Any help is greatly appreciated!

It is going to be extremely difficult to work out the object’s velocity from the applied force. I’ve been there - avoid it if you can. This is a Box2D thing and incredibly complex.

If you want to know what the velocity is, you should simply set it:

http://docs.coronalabs.com/daily/api/type/Body/setLinearVelocity.html

http://docs.coronalabs.com/daily/api/type/Body/angularVelocity.html

Hey, thanks for the advice. Instead of using applyForce I used setLinerVelocity with my randomly generated numbers. However my object still has varying velocities. Do you know of a way that I could set a global velocity value and split that number into x and y components of varying values. Then randomly decide if each one is either positive or negative?

Thanks for the help!

I still have had no luck. Is there any way to give an object x velocity at y degrees? I want to make my object go in random directions but at the same velocity? So can I choose a number 0 - 360, use the number to give my object a certain direction, and then give that object a certain velocity?

Thanks for any help

You can do it like this:
 

local xSpeed = math.cos(degree\*math.pi/180) local ySpeed = math.sin(degree\*math.pi/180) obj:setLinearVelocity(xSpeed\*objSpeed, ySpeed\*objSpeed);

objSpeed is the general speed at which you would like the object to move (:

Thanks! That seemed to do the trick. Just one thing. For the most part, the velocity of the object looks the same. However there is maybe like 1 in every 10 repetitions it looks a bit slower. Is this from the code or is just an illusion (going from top to bottom may look longer than going from side to side).

Thanks again!

It should be an illusion :D, maybe given from the ratio width/height of the screen. I guess you can make sure of it if you place a background with a grid, might be easier to check if it’s going faster/slower (;

Haha alright thanks again! :smiley:

It is going to be extremely difficult to work out the object’s velocity from the applied force. I’ve been there - avoid it if you can. This is a Box2D thing and incredibly complex.

If you want to know what the velocity is, you should simply set it:

http://docs.coronalabs.com/daily/api/type/Body/setLinearVelocity.html

http://docs.coronalabs.com/daily/api/type/Body/angularVelocity.html

Hey, thanks for the advice. Instead of using applyForce I used setLinerVelocity with my randomly generated numbers. However my object still has varying velocities. Do you know of a way that I could set a global velocity value and split that number into x and y components of varying values. Then randomly decide if each one is either positive or negative?

Thanks for the help!

I still have had no luck. Is there any way to give an object x velocity at y degrees? I want to make my object go in random directions but at the same velocity? So can I choose a number 0 - 360, use the number to give my object a certain direction, and then give that object a certain velocity?

Thanks for any help

You can do it like this:
 

local xSpeed = math.cos(degree\*math.pi/180) local ySpeed = math.sin(degree\*math.pi/180) obj:setLinearVelocity(xSpeed\*objSpeed, ySpeed\*objSpeed);

objSpeed is the general speed at which you would like the object to move (:

Thanks! That seemed to do the trick. Just one thing. For the most part, the velocity of the object looks the same. However there is maybe like 1 in every 10 repetitions it looks a bit slower. Is this from the code or is just an illusion (going from top to bottom may look longer than going from side to side).

Thanks again!

It should be an illusion :D, maybe given from the ratio width/height of the screen. I guess you can make sure of it if you place a background with a grid, might be easier to check if it’s going faster/slower (;

Haha alright thanks again! :smiley: