Re: "Questions that don't deserve their own thread" thread
Posted: Mon Sep 08, 2014 12:56 pm
Trying to get collision working in my game, I've been using the PhysicsCallback tutorial. It tells me when any objects collide with each other, but I want a function that checks to see when two specific objects collide, so I coded the two functions "beginPlayerEnemyContact" and "endPlayerEnemyContact", but I can't get them to actually do anything. Any advice?
Code: Select all
function love.load()
playerimage = love.graphics.newImage("knight.gif")
playerrun1 = love.graphics.newImage("KnightRunRight1.png")
playerrun2 = love.graphics.newImage("KnightRunRight2.png")
playeridle1 = love.graphics.newImage("KnightIdle1.png")
playeridle2 = love.graphics.newImage("KnightIdle2.png")
tempenemyimage = love.graphics.newImage("fighter.gif")
x = 50
y = 50
speed = 300
health = 10
--physics engine below
love.physics.setMeter(64)
world = love.physics.newWorld(0, 200, true)
world:setCallbacks(beginContact, endContact, preSolve, postSolve)
text = ""
persisting = 0
player = {}
player.b = love.physics.newBody(world, 400, 200, "dynamic")
player.b:setMass(10)
player.s = love.physics.newCircleShape(10)
player.f = love.physics.newFixture(player.b, player.s)
player.f:setRestitution(0.4)
player.f:setUserData("Knight")
enemy = {}
enemy.b = love.physics.newBody(world, 300, 200, "dynamic")
enemy.b:setMass(10)
enemy.s = love.physics.newCircleShape(10)
enemy.f = love.physics.newFixture(enemy.b, enemy.s)
enemy.f:setRestitution(0.4)
enemy.f:setUserData("Enemy")
static = {}
static.b = love.physics.newBody(world, 400, 400, "static")
static.s = love.physics.newRectangleShape(200,50)
static.f = love.physics.newFixture(static.b, static.s)
static.f:setUserData("Block")
love.graphics.setBackgroundColor(104, 136, 248)
love.window.setMode(650,650)
end
function beginContact(a, b, coll)
x,y = coll:getNormal()
text = text.."\n"..a:getUserData().." colliding with "..b:getUserData().." with a vector normal of: "..x..", "..y
end
function beginPlayerEnemyContact(player, enemy, coll)
x,y = coll:getNormal()
text = "!!!!!!!"
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function endPlayerEnemyContact(player, enemy, coll)
persisting = 0
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function endContact(a, b, coll)
persisting = 0
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function preSolve(a, b, coll)
if persisting == 0 then -- only say when they first start touching
text = text.."\n"..a:getUserData().." touching "..b:getUserData()
elseif persisting < 20 then -- then just start counting
text = text.." "..persisting
end
persisting = persisting + 1 -- keep track of how many updates they've been touching for
end
function postSolve(a, b, coll, normalimpulse1, tangentimpulse1, normalimpulse2, tangentimpulse2)
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("right") then
player.b:applyForce(100, 0)
elseif love.keyboard.isDown("left") then
player.b:applyForce(-100, 0)
end
if love.keyboard.isDown("up") then
player.b:applyForce(0, -500)
elseif love.keyboard.isDown("down") then
player.b:applyForce(0, 100)
end
if string.len(text) > 768 then
text = ""
end
end
function love.draw()
love.graphics.print(text, 10, 10)
love.graphics.draw(playerimage, player.b:getX(),player.b:getY())
love.graphics.draw(tempenemyimage, enemy.b:getX(),enemy.b:getY())
love.graphics.polygon("line", static.b:getWorldPoints(static.s:getPoints()))
end