# physics rect

hi this is maybe a noob question but… yeah…
i am learning physics right now and i know how to make a physics circle, but dont know how to make a rect…
can somebody help me

physics.polygon. just give it the corner x,y values. vec2(x,y). The physics.body documentation gives an example.

If you are going to rotate it when you draw, then you want the centre to be at 0,0. This is why in the documentation example, all the corners are equidistant from the centre, eg the bottom left corner is -w/2,-h/2 , where w=width, h= height

@dave1707 @Ignatz thanks but can you explain what vecs are?

Check in the codea doc.

this is my code at the moment…

``````-- physics rect

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
p_rect = physics.body(POLYGON,vec2(-10,10),vec2(-10,-10),vec2(10,-10),vec2(10,10))
p_rect.x = 350
p_rect.y = 350
end

function draw()
background(40, 40, 50)
strokeWidth(5)
stroke(255, 0, 0, 255)
fill(0, 6, 255, 255)
rect(p_rect.x,p_rect.y,100)
end

``````

it isnt showing my rect on the screen…

You need to include an extra item in rect, for height. Also, your physics body is a square 20 pixels wide, but you are drawing a rectangle 100 pixels wide.

In answer to your question, vec2 is simply a pair of numbers x,y. Think of it as a position on the screen. So if I say a=vec2(100,200), then a.x=100 and a.y=200. Vec2’s have special functions that are useful, eg for calculating the distance between two of them.

I’ve modified your code to make it more general, so it’s easy to create a rectangular body of any size. I also added a translate and rotation command, so if your rectangle bounces off anything and roates, then it will spin around.

Note that when you run this, your rectangle falls down the screen. This is because you have gravity turned on, and there is no other physics object to collide with, so it just falls off the screen.

``````function setup()
w,h,x,y=100,200,350,350
r=CreateRect(w,h,x,y)
end

function CreateRect(w,h,x,y)
p_rect = physics.body(POLYGON,vec2(-w/2,h/2),vec2(-w/2,-h/2),vec2(w/2,-h/2),vec2(w/2,h/2))
p_rect.x = x
p_rect.y = y
end

function draw()
background(40, 40, 50)
fill(0, 6, 255, 255)
pushMatrix()
translate(p_rect.x,p_rect.y)
rotate(p_rect.angle)
rect(0,0,w,h)
popMatrix()
end

``````

@Ignatz thanks! I already know how to make a circle with walls around it so now i can do it with rects!