Physics Body Moves

I’m a newbie and am trying to figure out physics bodies. I would think that when I create one of these, it would sit still until told to move but it wanders around. The code below shows what I mean. It just tracks the Y position of the body. Is there some force involved by default?

function setup()
Ans1Body = physics.body(CIRCLE, 100)
Ans1Body.type = DYNAMIC
Ans1Body.x = 500
Ans1Body.y = 500
end

function draw()
background(40, 40, 50)
text (string.format (“Ans1Body.y = %d”, Ans1Body.y), 200, HEIGHT - 100)
end

function setup()

 Ans1Body = physics.body(CIRCLE, 100) Ans1Body.type = DYNAMIC
 Ans1Body.x = 500 
Ans1Body.y = 500 end

function draw() 
background(40, 40, 50) 

text (string.format ("Ans1Body.y = %d", Ans1Body.y), 200, HEIGHT - 100)
 end

Didn’t fix it, but I formatted it.

EDIT: Also, the physics.body’s have gravity in them by default, you can easily change this, though.

@Prynok Isn’t this proper formatting? :stuck_out_tongue:

function setup()
    Ans1Body = physics.body(CIRCLE, 100)
    Ans1Body.type = DYNAMIC
    Ans1Body.x = 500 
    Ans1Body.y = 500
end

function draw() 
    background(40, 40, 50) 
    
    text(string.format ("Ans1Body.y = %d", Ans1Body.y), 200, HEIGHT - 100)
 end

Also, it could be just text("Ans1Body.y = " .. Ans1Body.y), 200, HEIGHT - 100), couldn’t it?

Physics bodies have gravity, if that’s what you mean. You can control it with physics.gravity(). (physics.gravity(Gravity) also makes physics bodies fall with the iPad’s gravity sensor.)

Try adding

Ans1Body.gravityScale=0

to remove the effect of gravity

@DaveW - in my Codea book (via wiki link above) I have at least one chapter on physics

gravityScale fixed the problem. I had read the description of that parameter but interpreted it as meaning that, if set to 0, gravity wouldn’t affect the object. I should have tested.

The code looked OK when I pasted it in but reformatted when the message was posted. Is there a trick for doing this?

Thanks to all!

Ignatz, I have used your book on several occasions; it’s very helpful. Thanks.

@DaveW
to format the code on the forums, put
~~~
on the line before the code and also on the line after the code