Page 1 of 1

help with collisions

Posted: Sun Oct 14, 2012 3:04 am
by Squaar
I'm trying to make an asteroids clone and I cant seem to make the collisions between my bullets and the asteroids do anything. For the moment, im only trying to make the bullet disappear when it hits an asteroid. I don't have the asteroids set as dynamic bodies to make it easier to hit them for testing.

I've been using this tutorial: https://love2d.org/wiki/Tutorial:Physic ... nCallbacks

here is the file where I have the collision code:

Code: Select all

--called in love.load()
function world_load()
	--set up world
	love.graphics.setMode(1024, 768, false, true, 16)
	love.physics.setMeter(100)
	world = love.physics.newWorld(0,0,true)
	world:setCallbacks(beginContact, endContact, preSolve, postSolve)
	
	player_create()
end

--collisions
function beginContact(a, b, coll)
	aBody = a:getBody()
	bBody = b:getBody()
	
	debuxText = "collision"
	
	if aBody:getType() == "bullet" and bBody:getType() == "asteroid" then
		debuxText = "collisionA"
		aBody:setType("bullet_D")
		bBody:setType("asteroid_D")
	end
	
	if bBody:getType() == "bullet" and aBody:getType() == "asteroid" then
		debuxText = "collisionB"
		bBody:setType("bullet_D")
		aBody:setType("asteroid_D")
	end
end

function endContact(a, b, coll)
    debuxText = "collision"
end

function preSolve(a, b, coll)
    debuxText = "collision"
end

function postSolve(a, b, coll)
    debuxText = "collision"
end
also, Im having trouble with the objects looping around the screen. Sometimes it works fine, and other times they just pop up in weird places. So any help with that would also be appreciated

I am a cat. Meow.

Posted: Sun Oct 14, 2012 4:37 am
by Helvecta
Helvecta helps again!

Plop this bad boy in there to fix the looping issues. You mixed a whole bunch of Xs and Ys up:

Code: Select all

if player.body:getX() <0 then
		player.body:setPosition(love.graphics.getWidth()-1, player.body:getY())
	elseif player.body:getX() > love.graphics.getWidth() then
		player.body:setPosition(1, player.body:getY())
	end
	if player.body:getY() <0 then
		player.body:setPosition(player.body:getX(), love.graphics.getHeight()-1)
	elseif player.body:getY() > love.graphics.getHeight() then
		player.body:setPosition(player.body:getX(), 1)
	end
end
As for the collision problems,
Image

Re: help with collisions

Posted: Sun Oct 14, 2012 4:59 am
by Squaar
lol Thanks again!

Re: help with collisions

Posted: Sun Oct 14, 2012 6:35 am
by Helvecta
Helvecta helps again!

I can't believe I actually figured this out. Here you go, dude! The problems are fixed!

Thanks for letting me have the source of this so I could mess with it, it really helped me understand the concept of physics in LOVE!

Anyways, this is very sloppy, many things are out of place, and by making my changes I might have interfered with things you had planned, but this code does exactly what you want -- when a bullet collides with the asteroid, it disappears! If you want, I will explain this more in-depth, but I need some sleep. This project destroyed my mind!!! :crazy:

I think I did pretty damn good considering I've never touched love.physics and that it is apparently "not even remotely simple to use"!
Asteroids FIXED.love
(5.47 KiB) Downloaded 116 times

Re: help with collisions

Posted: Sun Oct 14, 2012 7:04 am
by Squaar
Dude, you're a pro.