File:world raycast callback example.png
Size of this preview: 768 × 599 pixels. Other resolutions: 308 × 240 pixels | 806 × 629 pixels.
Original file (806 × 629 pixels, file size: 41 KB, MIME type: image/png)
A screenshot of a World:rayCast example for LÖVE 0.8.0.
function worldRayCastCallback(fixture, x, y, xn, yn, fraction)
local hit = {}
hit.fixture = fixture
hit.x, hit.y = x, y
hit.xn, hit.yn = xn, yn
hit.fraction = fraction
table.insert(Ray.hitList, hit)
return 1 -- Continues with ray cast through all shapes.
end
function createStuff()
-- Cleaning up the previous stuff.
for i = 1, #Terrain.Stuff do
Terrain.Stuff[i].Fixture:destroy()
Terrain.Stuff[i] = nil
end
-- Generates some random shapes.
for i = 1, 30 do
local p = {}
p.x, p.y = math.random(100, 700), math.random(100, 500)
local shapetype = math.random(3)
if shapetype == 1 then
local w, h, r = math.random() * 10 + 40, math.random() * 10 + 40, math.random() * math.pi * 2
p.Shape = love.physics.newRectangleShape(p.x, p.y, w, h, r)
elseif shapetype == 2 then
local a = math.random() * math.pi * 2
local x2, y2 = p.x + math.cos(a) * (math.random() * 30 + 20), p.y + math.sin(a) * (math.random() * 30 + 20)
p.Shape = love.physics.newEdgeShape(p.x, p.y, x2, y2)
else
local r = math.random() * 40 + 10
p.Shape = love.physics.newCircleShape(p.x, p.y, r)
end
p.Fixture = love.physics.newFixture(Terrain.Body, p.Shape)
Terrain.Stuff[i] = p
end
end
function love.keypressed()
createStuff()
end
function love.load()
-- Setting this to 1 to avoid all current scaling bugs.
love.physics.setMeter(1)
-- Start out with the same random stuff each start.
math.randomseed(0xfacef00d)
World = love.physics.newWorld()
Terrain = {}
Terrain.Body = love.physics.newBody(World, 0, 0, "static")
Terrain.Stuff = {}
createStuff()
Ray = {
x1 = 0,
y1 = 0,
x2 = 0,
y2 = 0,
hitList = {}
}
end
function love.update(dt)
local now = love.timer.getMicroTime()
World:update(dt)
-- Clear fixture hit list.
Ray.hitList = {}
-- Calculate ray position.
local pos = (math.sin(now/4) + 1.2) * 0.4
Ray.x2, Ray.y2 = math.cos(pos * (math.pi/2)) * 1000, math.sin(pos * (math.pi/2)) * 1000
-- Cast the ray and populate the hitList table.
World:rayCast(Ray.x1, Ray.y1, Ray.x2, Ray.y2, worldRayCastCallback)
end
function love.draw()
-- Drawing the terrain.
love.graphics.setColor(1, 1, 1)
for i = 1, #Terrain.Stuff do
if Terrain.Stuff[i].Shape:getType() == "polygon" then
love.graphics.polygon("line", Terrain.Body:getWorldPoints( Terrain.Stuff[i].Shape:getPoints() ))
elseif Terrain.Stuff[i].Shape:getType() == "edge" then
love.graphics.line(Terrain.Body:getWorldPoints( Terrain.Stuff[i].Shape:getPoints() ))
else
local x, y = Terrain.Body:getWorldPoints(Terrain.Stuff[i].x, Terrain.Stuff[i].y)
love.graphics.circle("line", x, y, Terrain.Stuff[i].Shape:getRadius())
end
end
-- Drawing the ray.
love.graphics.setLineWidth(3)
love.graphics.setColor(1, 1, 1, .4)
love.graphics.line(Ray.x1, Ray.y1, Ray.x2, Ray.y2)
love.graphics.setLineWidth(1)
-- Drawing the intersection points and normal vectors if there were any.
for i = 1, #Ray.hitList do
local hit = Ray.hitList[i]
love.graphics.setColor(1, 0, 0)
love.graphics.print(i, hit.x, hit.y) -- print point№...
love.graphics.circle("line", hit.x, hit.y, 3)
love.graphics.setColor(0, 1, 0)
love.graphics.line(hit.x, hit.y, hit.x + hit.xn * 25, hit.y + hit.yn * 25)
end
end
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 09:27, 26 July 2012 | 806 × 629 (41 KB) | Antoniomoder (talk | contribs) | add love.graphics.print(i, hit.x, hit.y) -- print point№... | |
12:02, 6 May 2012 | 800 × 600 (32 KB) | Boolsheet (talk | contribs) | A screenshot of a World:rayCast example for LÖVE 0.8.0. <source lang="lua">function worldRayCastCallback(fixture, x, y, xn, yn, fraction) local hit = {} hit.fixture = fixture hit.x, hit.y = x, y hit.xn, hit.yn = xn, yn hit.fraction = fracti |
- You cannot overwrite this file.
File usage
The following 3 pages link to this file: