help with collisions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

help with collisions

Post 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
Attachments
Asteroids.love
(5.31 KiB) Downloaded 103 times
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

I am a cat. Meow.

Post 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
"Bump." -CMFIend420
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

Re: help with collisions

Post by Squaar »

lol Thanks again!
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: help with collisions

Post 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 115 times
Last edited by Helvecta on Sun Oct 14, 2012 7:06 am, edited 1 time in total.
"Bump." -CMFIend420
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

Re: help with collisions

Post by Squaar »

Dude, you're a pro.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests