-
Notifications
You must be signed in to change notification settings - Fork 2
/
e0101.lua
86 lines (62 loc) · 2.21 KB
/
e0101.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- Example: Mini Physics Callbacks
-- Updated 0.8.0 by Bartoleo
text = "No collision yet."
function love.load()
-- One meter is 32px in physics engine
love.physics.setMeter( 32 )
-- Create a world with standard gravity
world = love.physics.newWorld(0, 9.81*32, true)
-- Create the ground body at (0, 0) static
ground = love.physics.newBody(world, 0, 0, "static")
-- Create the ground shape at (400,500) with size (600,10).
ground_shape = love.physics.newRectangleShape(400, 500, 600, 10)
-- Create fixture between body and shape
ground_fixture = love.physics.newFixture( ground, ground_shape)
ground_fixture:setUserData("Ground") -- Set a string userdata
-- Load the image of the ball.
ball = love.graphics.newImage("images/love-ball.png")
-- Create a Body for the circle.
body = love.physics.newBody(world, 400, 200, "dynamic")
-- Attatch a shape to the body.
circle_shape = love.physics.newCircleShape(0,0, 32)
-- Create fixture between body and shape
fixture = love.physics.newFixture( body, circle_shape)
fixture:setUserData("Ball") -- Set a string userdata
-- Calculate the mass of the body based on attatched shapes.
-- This gives realistic simulations.
body:setMassData(circle_shape:computeMass( 1 ))
-- Set the collision callback.
world:setCallbacks(beginContact,endContact)
end
function love.update(dt)
-- Update the world.
world:update(dt)
end
function love.draw()
-- Draws the ground.
love.graphics.polygon("line", ground_shape:getPoints())
-- Draw the circle.
love.graphics.draw(ball,body:getX(), body:getY(), body:getAngle(),1,1,32,32)
-- Instructions
love.graphics.print("space: Apply a random impulse",5,5)
-- Draw text.
love.graphics.print(text, 5, 25)
end
function love.keypressed(k)
if k == " " then
-- Apply a random impulse
body:applyLinearImpulse(150-math.random(0, 300),-math.random(0, 1500))
end
end
-- This is called every time a collision begin.
function beginContact(a, b, c)
local aa=a:getUserData()
local bb=b:getUserData()
text = "Collided: " .. aa .. " and " .. bb
end
-- This is called every time a collision end.
function endContact(a, b, c)
local aa=a:getUserData()
local bb=b:getUserData()
text = "Collision ended: " .. aa .. " and " .. bb
end