# Physics

Can somebody explain a bit how physic bodies work?
And how do you create a simple circle that falls on the floor, can somebody make a example for me?

``````-- physicaesample

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
circle =  physics.body(CIRCLE, 100)
circle.x = 400
circle.y= 300
circle.physicsScale = .100
floor = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
end

-- This function gets called once every frame
function draw()

-- This sets a dark background color
background(40, 40, 50)
sprite("Documents:ExampleCircle", circle.x, circle.y)
-- This sets the line thickness
strokeWidth(5)

end
``````

So basically, we created a physics.body, and made it a circle, with a radius of 100, next we put its x and y at certain positions, and gave it a physicsScale of .100, think of that as the “weight” of the circle. The floor is a line that goes across the bottom of the screen (it requires two vectors.) Then in the draw() function, we made a sprite and set the position of it to circle.x and circle.y. Tell us if you need any more explanation

@Prynok but what are vectors exactly?

Think of vectors as coordinates, They have to have a x and a y value. They are very useful because you don’t need tons of variables, and you can make your code shorter. Here is an example of a vector:

``````function setup()

vector = vec2(100, 200)

end

function draw()

sprite("", vector.x, vector.y)
end
``````

EDIT: There are two types of vectors, vec2 and vec3, vec2 is used for a 2d project, while vec3 is used for 3d. I explained above how to use a vec2, but to use a vec3 you need three vaules, an x, y, and z. But for now a vec2 should suit you.

Also see the physics documentation and the “Physics Lab” example.

I’ve blogged on physics (see in here).
http://coolcodea.wordpress.com/2013/06/19/index-of-posts/#more-8884

And partly written a Codea ebook that has a chapter on physics.
https://www.dropbox.com/s/t5im6tl14ky5t08/Codea%20for%20beginners.pdf

this is a complete example for physics.body,physics.joint,REVOLUTE,DISTANCE,PRISMATIC, applyForce, etc

http://pastebin.com/hZgjDbBm

Thanks everybody i will try them out

@Prynok There’s also vec4, which holds x, y, z, and w.

vec2 isn’t specific to 2D, and vec3 isn’t specific to 3D. (Though they generally would, you can use both in 2D or both in 3D. Also, vec4 would generally be 3D too, but again, it’s not restricted to only 3D.)

Also, I believe vec3 also has r, g, b, and vec4 also has r, g, b, a. If you change one it changes the other, I think.

@SkyTheCoder I know they aren’t just used for 2d and 3d, but I was thinking that would be the simplest way to say it.