how can I set the Linear Velocity to be constant?

So I have a line of code that moves my box.

box:setLinearVelocity(0,-250)

this moves the box when I click my button but then I have to release the button and press again for it to go any further. How can I make it so it is constantly going while my finger is on the button?

thanks!

I believe you have to change the physics properties of “box”.

physics.addBody( box, { density=0, friction=0, bounce=0})

you may want to try playing around with functions for your box 

eg:

box:applyForce( xForce, yForce, bodyX, bodyY )

or

box:applyLinearImpulse()

etc refer to the documentation on these functions to understand them aswell.

James 

I have a similar question as Max.

But instead of having my finger on anything, I want my object to bounce around slowly without any interaction at all…Tried so many things so far. I dont get any errors, but the behavior is always wrong. Either it moves too fast or it spin when it moves, lol…Im looking for more of a “floating” type of behavior.

I have a picture of a cup cake that Im using as my main object and for my background I just have a picture of outer space…I already have my boundaries set and everything else works great…Just need to get things “moving”…PLEASE HELP ME MAKE MY CUPCAKE FLOAT IN OUTERSPACE! :lol:

Thanks for any help. B)

Do you want the movement to be random or in a pattern bouncing around in space?

This is how i would do it:

  • Make tansparent lines 30 pixels thick and the length of the screen boundaries for the boundaries and position them correctly using x and y coordinates.

  • add shape table to the images of the transparent lines

  • add physics body and put the shape within the physics body on the lines but make sure you define the lines as static.

  • take your cupcake and add a physics body to it

  • use “cupcake:applyLinearImpulse()” to send it flying and bouncing of these boundaries.

If you really desperately need help feel free to post your source code and i will help you out :) 

James

Well, actually…

Can you explain to me how to correctly use the “shape” parameter? The docs say -

http://docs.coronalabs.com/api/library/physics/addBody.html

Shape array containing the shape’s vertices: { x1,y1, x2,y2, …, xn,yn }. For example pentagonShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }. The coordinates must be defined in clockwise order, and the resulting shape must be convex at all angle points. The physics engine assumes that the 0,0 point is the center of the object. A negative x will be to the left of object’s center and a negative y will be top of object’s center.

Where it says “pentagonShape”, how do those number make a pentagon? This is the part I dont understand. Im trying to type in values using the shape parameter, that form a square shape…Any help appreciated. Thanks.

Was interested in this as  future project of mine will be physics related - so just tried something and think it may help you understand.

[lua]

local penC = display.newRoundedRect( 0, 0, 10, 10, 0)

penC:setFillColor(0, 0, 1)

penC.x = 160

penC.y = 240

– sceneGroup:insert( penC )

– { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }. 

local pen1 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen1:setFillColor(0, 0, 1)

pen1.x = penC.x

pen1.y = penC.y - 37

– sceneGroup:insert( pen1 )

local pen2 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen2:setFillColor(0, 0, 1)

pen2.x = penC.x + 37

pen2.y = penC.y - 10

– sceneGroup:insert( pen2 )

local pen3 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen3:setFillColor(0, 0, 1)

pen3.x = penC.x + 23

pen3.y = penC.y + 34

– sceneGroup:insert( pen3 )

local pen4 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen4:setFillColor(0, 0, 1)

pen4.x = penC.x - 23

pen4.y = penC.y + 34

– sceneGroup:insert( pen4 )

local pen5 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen5:setFillColor(0, 0, 1)

pen5.x = penC.x - 37

pen5.y = penC.y - 10

– sceneGroup:insert( pen5 )

[/lua]

load this code into a main.lua file and it should display dots which if you drew lines between would form a pentagon.

Play with the fill colour for each one.

T.

Okay so the shape array works like this:

(0,0) is the center of the image yes. let’s say you have a rectangle which is 200 pixels wide by 100 pixels in height:

I always start by mapping the top left point. Remember all these coordinates are from the perspective that the center of object is 0,0

rectangleShape = {-100, 50, 100, 50, 100, -50, -100, -50}

the first -100, 50 maps the top left hand point of the rectangle as its saying from the center the top left part of the rectangle is 100 pixels left of the center and 50 pixels up they y axis.

the second point (100,50) maps the top right, 100 pixels to the right of the center of the object and 50 points up the y axis.

so on so forth mapping all 4 corners of the rectangle

James

I believe you have to change the physics properties of “box”.

physics.addBody( box, { density=0, friction=0, bounce=0})

you may want to try playing around with functions for your box 

eg:

box:applyForce( xForce, yForce, bodyX, bodyY )

or

box:applyLinearImpulse()

etc refer to the documentation on these functions to understand them aswell.

James 

I have a similar question as Max.

But instead of having my finger on anything, I want my object to bounce around slowly without any interaction at all…Tried so many things so far. I dont get any errors, but the behavior is always wrong. Either it moves too fast or it spin when it moves, lol…Im looking for more of a “floating” type of behavior.

I have a picture of a cup cake that Im using as my main object and for my background I just have a picture of outer space…I already have my boundaries set and everything else works great…Just need to get things “moving”…PLEASE HELP ME MAKE MY CUPCAKE FLOAT IN OUTERSPACE! :lol:

Thanks for any help. B)

Do you want the movement to be random or in a pattern bouncing around in space?

This is how i would do it:

  • Make tansparent lines 30 pixels thick and the length of the screen boundaries for the boundaries and position them correctly using x and y coordinates.

  • add shape table to the images of the transparent lines

  • add physics body and put the shape within the physics body on the lines but make sure you define the lines as static.

  • take your cupcake and add a physics body to it

  • use “cupcake:applyLinearImpulse()” to send it flying and bouncing of these boundaries.

If you really desperately need help feel free to post your source code and i will help you out :) 

James

Well, actually…

Can you explain to me how to correctly use the “shape” parameter? The docs say -

http://docs.coronalabs.com/api/library/physics/addBody.html

Shape array containing the shape’s vertices: { x1,y1, x2,y2, …, xn,yn }. For example pentagonShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }. The coordinates must be defined in clockwise order, and the resulting shape must be convex at all angle points. The physics engine assumes that the 0,0 point is the center of the object. A negative x will be to the left of object’s center and a negative y will be top of object’s center.

Where it says “pentagonShape”, how do those number make a pentagon? This is the part I dont understand. Im trying to type in values using the shape parameter, that form a square shape…Any help appreciated. Thanks.

Was interested in this as  future project of mine will be physics related - so just tried something and think it may help you understand.

[lua]

local penC = display.newRoundedRect( 0, 0, 10, 10, 0)

penC:setFillColor(0, 0, 1)

penC.x = 160

penC.y = 240

– sceneGroup:insert( penC )

– { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }. 

local pen1 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen1:setFillColor(0, 0, 1)

pen1.x = penC.x

pen1.y = penC.y - 37

– sceneGroup:insert( pen1 )

local pen2 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen2:setFillColor(0, 0, 1)

pen2.x = penC.x + 37

pen2.y = penC.y - 10

– sceneGroup:insert( pen2 )

local pen3 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen3:setFillColor(0, 0, 1)

pen3.x = penC.x + 23

pen3.y = penC.y + 34

– sceneGroup:insert( pen3 )

local pen4 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen4:setFillColor(0, 0, 1)

pen4.x = penC.x - 23

pen4.y = penC.y + 34

– sceneGroup:insert( pen4 )

local pen5 = display.newRoundedRect( 0, 0, 10, 10, 0)

pen5:setFillColor(0, 0, 1)

pen5.x = penC.x - 37

pen5.y = penC.y - 10

– sceneGroup:insert( pen5 )

[/lua]

load this code into a main.lua file and it should display dots which if you drew lines between would form a pentagon.

Play with the fill colour for each one.

T.

Okay so the shape array works like this:

(0,0) is the center of the image yes. let’s say you have a rectangle which is 200 pixels wide by 100 pixels in height:

I always start by mapping the top left point. Remember all these coordinates are from the perspective that the center of object is 0,0

rectangleShape = {-100, 50, 100, 50, 100, -50, -100, -50}

the first -100, 50 maps the top left hand point of the rectangle as its saying from the center the top left part of the rectangle is 100 pixels left of the center and 50 pixels up they y axis.

the second point (100,50) maps the top right, 100 pixels to the right of the center of the object and 50 points up the y axis.

so on so forth mapping all 4 corners of the rectangle

James