Physics demo with GetRestitution typo fix

Showcase your libraries, tools and other projects that help your fellow love users.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Physics demo with GetRestitution typo fix

Post by pekka »

Sorry for not replying earlier and still dragging this on a bit. I'm fine with this being locked, but I'm going to post a slightly changed version of my code here first. It has some changes, the major one being that you can change the size of the objects being created, so it is even more fun than before! It could amuse you all for... minutes. I tell you, minutes. Very short minutes. Very short and AMUSING minutes.

You can use the mouse wheel to change the size that new objects will have.

Anyway, since this should now count as the cleaned up version, I'll remove the two earlier ones by editing my messages yet again. And since I had to write that thing about licenses before, I'm still putting a license on this. It's now the MIT License, and I am going to stick to that from now on.

So. It's OK to lock this thread. Perhaps you could wait a few days for any last comments if people still want to add something. It's really up to you. I'm pretty sure I'm done by now.

Also, no kicking or screaming was involved. I'm just being busy with hacking at my little Lovefest game with what free time I have. I better pay a little more attention to the code I am going to post in the future, though.

I have no opposition to using the logical or as above. I just prefer the if-then-else style myself, so I'll stick with it for this example, since it is already in the code. And you know what will happen if I try to make some last-minute edits before posting code... Horrors will happen.

Code: Select all

local license = [[

Copyright (c) 2010 Pekka Karjalainen

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
]]

local drawables = {}
local groundbody = nil
local groundshape = nil
local curSize = 40
local timeAcc = 0
local timeStep = 1/60
local TWOPI = math.pi * 2
local TGTFPS = 90
local lastframe = nil

function love.load()
	world = love.physics.newWorld(-100,-100, 900, 700, 0, 20)
	groundbody = love.physics.newBody(world, 400, 570)
	groundshape = love.physics.newRectangleShape(groundbody, 0, 0, 600, 40)
	lastframe = love.timer.getMicroTime()
end

function love.keypressed(k)
	if k == 'escape' then
        	love.event.push('q')
        end
end

function love.draw()
	local haveGR = true
	local triangleMessage = ''
        for _, d in ipairs(drawables) do
		local name, body, shape, size = unpack(d)
		if shape.getRestitution == nil then haveGR = false end
		local x, y = body:getWorldCenter()
		local angle = body:getAngle()
		if name == "circle" then
			local rad = size/2
			love.graphics.circle("line", x, y, rad, 32)
			love.graphics.line(x, y, x + math.cos(angle) * rad,
					y + math.sin(angle) * rad)
		end
		if name == "square" then
			local mul = math.sqrt(size^2/2) -- from center to corner
			for r = 0, 3 do
				r = r - 0.5 -- correct orientation
				love.graphics.line(x + math.cos(angle + r/4*TWOPI)*mul,
					 	   y + math.sin(angle + r/4*TWOPI)*mul,
						   x + math.cos(angle + (r+1)/4*TWOPI)*mul,
						   y + math.sin(angle + (r+1)/4*TWOPI)*mul)
			end
		end
		if name == "triangle" then
			local mul = size * math.sqrt(3)/3 -- r of circumscribed circle
			for r = 0, 2 do
				r = r + 3/4 -- correct orientation
				love.graphics.line(x + math.cos(angle + r/3*TWOPI)*mul,
						   y + math.sin(angle + r/3*TWOPI)*mul,
						   x + math.cos(angle + (r+1)/3*TWOPI)*mul,
						   y + math.sin(angle + (r+1)/3*TWOPI)*mul)
			end
		end
	end
	x, y = groundbody:getPosition()
	love.graphics.rectangle("fill", x-300, y-20, 600, 40)
	love.graphics.print(tostring(#drawables) .. " objects created", 10, 20)
	if haveGR and #drawables > 0 then
		love.graphics.print("and they all have a getRestitution method", 10, 40)
	end
	love.graphics.print('current object size: ' .. curSize, 10, 60)
end

function love.update(dt)
	timeAcc = timeAcc + dt
	while timeAcc > timeStep do
		timeAcc = timeAcc - timeStep
		world:update(timeStep)
	end
	local sleep = 1000/TGTFPS - 1000 * (love.timer.getMicroTime() - lastframe)
        if sleep > 0 then love.timer.sleep(sleep) end
        lastframe = love.timer.getMicroTime()
end

local circleCreated = false
local polygonCreated = false

function love.mousepressed( x, y, button )
	if button == 'wd' then curSize = curSize - 1 end
	if button == 'wu' then curSize = curSize + 1 end
	if button == 'wd' or button == 'wu' then
		if curSize < 5 then curSize = 5 end
		if curSize > 100 then curSize = 100 end
		return
	end
	local body = love.physics.newBody(world, x, y)
	local shape, name = nil, nil
	if button == 'l' then
		shape = love.physics.newCircleShape(body, 0, 0, curSize/2)
		name = "circle"
		if circleCreated == false then
			circleCreated = true
			mt = getmetatable(shape)
			if mt.getRestitution == nil then
				mt.getRestitution = mt.getRestituion
			end
		end
	end
	if button == 'r' then
		shape = love.physics.newRectangleShape(body, 0, 0, curSize, curSize)
		name = "square"
		if polygonCreated == false then
			polygonCreated = true
			mt = getmetatable(shape)
			if mt.getRestitution == nil then
				mt.getRestitution = mt.getRestituion
			end
		end
	end
	if button == 'm' then
		local ela = curSize*math.sqrt(3)/2 -- altitude of an equilateral triangle
		shape = love.physics.newPolygonShape(body, -curSize/2, -ela/3,
				curSize/2, -ela/3, 0, 2*ela/3)
		name = "triangle"
		if polygonCreated == false then
			polygonCreated = true
			mt = getmetatable(shape)
			if mt.getRestitution == nil then
				mt.getRestitution = mt.getRestituion
			end
		end
	end
	shape:setDensity(50)
	body:setMassFromShapes()
	table.insert(drawables, {name, body, shape, curSize})
end

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], alexjgriffith and 4 guests