# Weak physics

I made a little physics engine, and I could use a few improvements or suggestions.

``````
--# Main
function setup()
displayMode(FULLSCREEN)
velocity = vec2(0,0)
SpacecraftPos = vec2(WIDTH/2, HEIGHT/2)
Grav = vec2(0,1)
DeBounce=9.4
Fric=10/60
end

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

--Gravity
Grav = vec2(-Gravity.x, -Gravity.y)
velocity=velocity-Grav
SpacecraftPos = SpacecraftPos + velocity

Collision()
RenderShip()
end

function touched(touchData)
if touchData.state==MOVING then
velocity = vec2(touchData.deltaX,touchData.deltaY)
elseif touchData.state==BEGAN then
velocity = velocity + Grav*20
end
end

function Collision()
Collide = false
Side = false
--Sides
if SpacecraftPos.x < 0 then
velocity.x = (-velocity.x)-Grav.x*DeBounce
SpacecraftPos.x = 0
Collide = true
Side = false
end
if SpacecraftPos.x > WIDTH then
velocity.x = (-velocity.x)-Grav.x*DeBounce
SpacecraftPos.x = WIDTH
Collide = true
Side = false
end
if SpacecraftPos.y > HEIGHT then
velocity.y = (-velocity.y)-Grav.y*DeBounce
SpacecraftPos.y = HEIGHT
Collide = true
Side = true
end
if SpacecraftPos.y < 0 then
velocity.y = (-velocity.y)-Grav.y*DeBounce
SpacecraftPos.y = 0
Collide = true
Side = true
end

--Friction
if Collide == true then
if Side == true then
if velocity.x == math.abs(velocity.x) then
velocity.x = velocity.x -Fric
else
velocity.x = velocity.x +Fric
end
end
end
end

function RenderShip()
if Grav.x < 0 then
sprite("Space Art:Red Ship Bank Right", SpacecraftPos.x, SpacecraftPos.y)
else
sprite("Space Art:Red Ship Bank Left", SpacecraftPos.x, SpacecraftPos.y)
end
end
``````

@LuaLlama You did a good job and you probably understand the calculations you needed to do that. Your next step is to learn to use the physics engine that does all the calculations for you. Below is code I put together using the physics engine to do approximately what you’re doing. As you can see, there’s a lot less code and the path of the ship is more realistic as it bounces around.

``````displayMode(FULLSCREEN)

function setup()
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e2=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
e3=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
e4=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
b=physics.body(CIRCLE,40)
b.x=WIDTH/2
b.y=HEIGHT/2
b.restitution=1
physics.continuous=true
end

function draw()
background(40, 40, 50)
sprite("Space Art:Red Ship",b.x,b.y)
end

function touched(t)
if t.state==MOVING then
b.linearVelocity=vec2(t.deltaX*20,t.deltaY*20)
end
end
``````

@dave1707 Thanks! I sure will take a look at codea’s physics now. Thanks again!

There’s no harm making your own, sometimes you only need a small part of the engine or want to tweak it.

I strongly recommend reading this great ebook, which has some great explanations and code in Processing, which is a language very similar to Codea.

http://natureofcode.com/book/

I suggest using the built in Codea physics. I spent a lot of time trying to “fake” physics and trying to figure out the built in physics but once I could figure out the built in physics it was worth. Try looking at the discussions I created about an air hockey game @LuaLlama