# Physics question

The physics lab example has an elaborate routine to handle collisions, working out worldAnchors and applying a complicated force, but why? The simple code below seems to handle collisions ok without any special code. I don’t understand when you would need the elaborate version.

``````function setup()
--create objects

--first the rectangle
rect_width=100 --size of rectangle
rect_height=50
--now create the physics object that behaves like a real block, it will tell us where to draw our rectangle
--think of it as a GPS navigator that guides us
p_rect = physics.body(POLYGON, vec2(-rect_width / 2, -rect_height / 2),vec2(-rect_width / 2, rect_height / 2),vec2(rect_width / 2, rect_height / 2),vec2(rect_width / 2, -rect_height / 2))
p_rect.x = math.random(60,250) -- choose a random position between 30 and the screen width minus 30
p_rect.y = math.random(60,250) -- same for the y position
p_rect.angle=math.random(0,360) -- and rotation
p_rect.gravityScale = 0 -- no gravity - if there was, the ball would fall to the bottom of screen
p_rect.restitution = 1 -- this circle is bouncy
p_rect.friction = 0.1 -- the amount of friction to be applied to the circle
p_rect.linearVelocity = vec2(100+math.random(400),100+math.random(400))  --velocity (pixels per second = 60 redraws)
--p_rect.linearDamping = 0.2
--p_rect.angularDamping = 0.2

p_rect.info="rect" --so when collisions occur, we can figure out which object was involved

--now much the same for the image
img_diam=(img.width+img.height)/2 --use average of width and height as diameter
--create the physics object
p_img = physics.body(CIRCLE,img_diam/2) --physics bodies use radius not diameter
p_img.x = math.random(60,250) -- choose a random position
p_img.y = math.random(60,250) -- same for the y position
p_img.angle=math.random(0,360)
p_img.gravityScale = 0 -- no gravity
p_img.restitution = 0.8 -- this image is not so bouncy
p_img.friction = 0.4 -- the amount of friction to be applied to the image
p_img.linearVelocity = vec2(math.random(400),math.random(400))  -- velocity
p_img.info="mine" --so when collisions occur, we can figure out which object was involved
--p_img.linearDamping = 0.9 -- WHAT DO THESE DO?
--p_img.angularDamping = 0.9

--finally the walls
CreateWalls()
end

--create the walls - physics objects can only bounce off each other
function CreateWalls()
leftWall = physics.body(EDGE,vec2(1,1),vec2(1,HEIGHT-1)) -- vec2 is a "vector" consisting of an x,y pair
rightWall = physics.body(EDGE,vec2(WIDTH-1,0),vec2(WIDTH-1,HEIGHT-1)) -- HEIGHT and WIDTH give us screen dimensions
bottomWall = physics.body(EDGE,vec2(1,1),vec2(WIDTH-1,1))
topWall = physics.body(EDGE,vec2(1,HEIGHT-1),vec2(WIDTH-1,HEIGHT-1))
--set the bounciness of the walls
leftWall.restitution=1.0 --no slowing
rightWall.restitution=0.9 --some slowing
bottomWall.restitution=0.2
rightWall.restitution=0.7
end

function draw()
background(200,200,200,255)

--draw the rectangle --
pushStyle()
fill(255,0,0,255)
pushMatrix()
translate(p_rect.x, p_rect.y)
rotate(p_rect.angle)
rect(-rect_width / 2, -rect_height / 2, rect_width, rect_height)
popMatrix()
popStyle()
-------------------------------

--draw image ---
pushMatrix()
translate(p_img.x, p_img.y)
rotate(p_img.angle)
sprite(img,0,0)
popMatrix()
-------------------------------

end
``````

I don’t know which elaborate collision handling you mean. And the elaborate function around “worldAnchors” is there so you can move objects around like on a rubber band by touching and dragging them.

The basics are indeed worked out by the physics engine.

I think what you are saying is the complex code in debugPhyics is there for the more complex physics actions. The problem is that it is very offputting for noobs, who don’t realise you don’t need any of it for many physics actions.