# physice

players should be able to stand on a block-please send me code thank you

Haha, I just coded a small example for physics anyways. Here it is. It covers some of the basics of physics. It does use a fancy drawing class, though. You can ignore that part.

``````
-- Physics Simplified

-- Use this function to perform your initial setup
function setup()
debugDraw = PhysicsDebugDraw() -- Create an instance of a class that draws our physics objects. (This would have to be handled manually, usually.)

circle = physics.body(CIRCLE, 25) -- Create a circle physics object

circle.x, circle.y = WIDTH / 2, HEIGHT / 2 -- Set the X and Y positions of the circle

circle.linearVelocity = vec2(100, 250) -- Set the velocity of the circle so that it moves to the right and up

circle.restitution = 0.5 -- This is how bouncy the circle is, 1 being it always bounces up to the same height, 0 being it doesn't bounce back up at all. 0.5 is in the middle, where it bounces up only half the height from where it came from

debugDraw:addBody(circle) -- Add the circle to the PhysicsDebugDraw's object cache so it knows to draw it.

square = physics.body(POLYGON, vec2(0, 0), vec2(0, 50), vec2(50, 50), vec2(50, 50), vec2(50, 0)) -- There's actually no preset for a square, so we have to make a polygon shape with square vertices.

square.x, square.y = WIDTH / 3, HEIGHT / 2 -- Set the square's position

walls = physics.body(CHAIN, true, vec2(20, 20), vec2(20, HEIGHT - 20), vec2(WIDTH - 20, HEIGHT - 20), vec2(WIDTH - 20, 20), vec2(20, 20)) -- This one's kinda complicated, it's a "chain" object, which cannot move, and is basically a bunch of points. It can work as borders for the screen, as otherwise the circle would fall off the edges.

debugDraw:addBody(walls) -- PhysicsDebugDraw can draw the walls, too.
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

debugDraw:draw() -- PhysicsDebugDraw can draw all the objects for us.
end

function touched(touch)
debugDraw:touched(touch) -- PhysicsDebugDraw has a nice system so that you can drag around an object with your fingers.
end

function collide(contact)
debugDraw:collide(contact) -- A nice little red spot pops up to show you where a collision is.
end

-- This is a really advanced bit of code from the physics project, used for drawing physics objects. I wouldn't try to begin to understand it if I were you.
PhysicsDebugDraw = class()

function PhysicsDebugDraw:init()
self.bodies = {}
self.joints = {}
self.touchMap = {}
self.contacts = {}
end

table.insert(self.bodies,body)
end

table.insert(self.joints,joint)
end

function PhysicsDebugDraw:clear()
-- deactivate all bodies

for i,body in ipairs(self.bodies) do
body:destroy()
end

for i,joint in ipairs(self.joints) do
joint:destroy()
end

self.bodies = {}
self.joints = {}
self.contacts = {}
self.touchMap = {}
end

function PhysicsDebugDraw:draw()

pushStyle()
smooth()
strokeWidth(5)
stroke(128,0,128)

local gain = 2.0
local damp = 0.5
for k,v in pairs(self.touchMap) do
local worldAnchor = v.body:getWorldPoint(v.anchor)
local touchPoint = v.tp
local diff = touchPoint - worldAnchor
local vel = v.body:getLinearVelocityFromWorldPoint(worldAnchor)
v.body:applyForce( (1/1) * diff * gain - vel * damp, worldAnchor)

line(touchPoint.x, touchPoint.y, worldAnchor.x, worldAnchor.y)
end

stroke(0,255,0,255)
strokeWidth(5)
for k,joint in pairs(self.joints) do
local a = joint.anchorA
local b = joint.anchorB
line(a.x,a.y,b.x,b.y)
end

stroke(255,255,255,255)
noFill()

for i,body in ipairs(self.bodies) do
pushMatrix()
translate(body.x, body.y)
rotate(body.angle)

if body.type == STATIC then
stroke(255,255,255,255)
elseif body.type == DYNAMIC then
stroke(150,255,150,255)
elseif body.type == KINEMATIC then
stroke(150,150,255,255)
end

if body.shapeType == POLYGON then
strokeWidth(3.0)
local points = body.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CHAIN or body.shapeType == EDGE then
strokeWidth(3.0)
local points = body.points
for j = 1,#points-1 do
a = points[j]
b = points[j+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CIRCLE then
strokeWidth(3.0)
end

popMatrix()
end

stroke(255, 0, 0, 255)
fill(255, 0, 0, 255)

for k,v in pairs(self.contacts) do
for m,n in ipairs(v.points) do
ellipse(n.x, n.y, 10, 10)
end
end

popStyle()
end

function PhysicsDebugDraw:touched(touch)
local touchPoint = vec2(touch.x, touch.y)
if touch.state == BEGAN then
for i,body in ipairs(self.bodies) do
if body.type == DYNAMIC and body:testPoint(touchPoint) then
self.touchMap[touch.id] = {tp = touchPoint, body = body, anchor = body:getLocalPoint(touchPoint)}
return true
end
end
elseif touch.state == MOVING and self.touchMap[touch.id] then
self.touchMap[touch.id].tp = touchPoint
return true
elseif touch.state == ENDED and self.touchMap[touch.id] then
self.touchMap[touch.id] = nil
return true;
end
return false
end

function PhysicsDebugDraw:collide(contact)
if contact.state == BEGAN then
self.contacts[contact.id] = contact
sound(SOUND_HIT, 2643)
elseif contact.state == MOVING then
self.contacts[contact.id] = contact
elseif contact.state == ENDED then
self.contacts[contact.id] = nil
end
end

``````

Please don’t go on the forums and literally ask someone to write half a game for you… Take a look at the “Physics Lab” project in the examples, and the physics API documentation.

You’re going to have to write most code for your self, the forums will only help with a small example or troubleshooting.

I’m beginner and would like to see the code so that I can learn in

There’s code to see in the examples and the documentation.

thanks