CollisionCallbacks/Contact

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
j_m_o_
Prole
Posts: 1
Joined: Fri Feb 14, 2020 11:17 pm

CollisionCallbacks/Contact

Post by j_m_o_ »

Hello,
I am pretty inexperienced. I have only taken and intro course to C and watched youtube videos.
I am trying to make a Brick Breaker clone to learn some of the basics. I have been watching the videos on youtube https://www.youtube.com/watch?v=rtV3uIuxNls&t=1489s. I found this tutorial, https://love2d.org/wiki/Tutorial:Physic ... nCallbacks, but can not understand what I am doing wrong. I have tried implementing the setUserData("Ball") several different ways and could not get them to work. I am guessing my issue is what would be considered classes in other languages.
Right now all I am trying to do is figure out a way to detect collisions and then have a way to determine the "bricks", which is what I am seeking help on. I know I have a lot more to figure out but I figured one step at a time. Here is the working code I have before implementing setUserData. Any help would be appreciated. Thank you.

conf.lua

Code: Select all

function love.conf(t)
	t.window.title = "Brick Breaker Clone"
    t.window.width = 1600
    t.window.height = 900
	t.window.fullscreen = false
end
Ball.lua

Code: Select all

Ball = {}

Ball.new = function(x,y,radius,physicsWorld)
	local self = self or {}
	self.x = x
	self.y = y
	self.radius = radius
	
	
	self.physics = {}
	self.physics.world = physicsWorld
	self.physics.body = love.physics.newBody(self.physics.world, self.x, self.y, "dynamic")
	self.physics.shape = love.physics.newCircleShape(self.radius) 
	self.physics.fixture = love.physics.newFixture(self.physics.body, self.physics.shape, 1)
	self.physics.fixture:setRestitution(1) -- "bouncyness"

	
	self.draw = function()
		love.graphics.circle("fill", self.physics.body:getX(), 
									 self.physics.body:getY(), 
									 self.radius)
	end
	
	
	-- moveLeft and moveRight added to test side borders
	self.moveLeft = function()
		self.physics.body:applyLinearImpulse(-800,0)
	end
	self.moveRight = function()
		self.physics.body:applyLinearImpulse(800,0)
	end
	
	
	return self
end
main.lua

Code: Select all

require("Ball")

local ball = nil

local physicsWorld = nil

local imageFile
local frames = {}

function love.load()

	love.keyboard.setKeyRepeat(true)

	physicsWorld = love.physics.newWorld(0,9.81*64, true)
	
	ball = Ball.new(love.graphics.getWidth()/2, 1, 20, physicsWorld)

	imageFile = love.graphics.newImage("Bricks.PNG")
	frames[1] = love.graphics.newQuad(0,0,  133,43, imageFile:getDimensions() )
	--[[
	frames[2] = love.graphics.newQuad(0,42,  67,43, imageFile:getDimensions() )
	frames[3] = love.graphics.newQuad(66,42,  67,43, imageFile:getDimensions() )
	frames[4] = love.graphics.newQuad(0,85,  133,43, imageFile:getDimensions() )
	frames[5] = love.graphics.newQuad(0,127,  133,43, imageFile:getDimensions() )
	--]]
	Borders()
	Brick()
end

function love.draw()
		
	ball:draw()
	
	love.graphics.draw(imageFile,frames[1],love.graphics.getWidth()/2-75,love.graphics.getHeight()/2-11)
	--[[
	love.graphics.draw(imageFile,frames[2],300,0)
	love.graphics.draw(imageFile,frames[3],400,0)
	love.graphics.draw(imageFile,frames[4],0,100)
	love.graphics.draw(imageFile,frames[5],400,100)
	--]]
end

function love.update(dt)
	physicsWorld:update(dt)
end

function love.keypressed(key)
	if(key == "a") then 
		ball:moveLeft()
	end
	if(key == "d") then
		ball:moveRight()
	end
end

function Brick()
	local brick = {}
	brick.body = love.physics.newBody(physicsWorld,love.graphics.getWidth()/2,love.graphics.getHeight()/2)
	brick.shape = love.physics.newRectangleShape(132/2,42/2)
	brick.fixture = love.physics.newFixture(brick.body,brick.shape)
end

function Borders()

	local floor = {}
	floor.body = love.physics.newBody(physicsWorld,love.graphics.getWidth()/2,love.graphics.getHeight())
	floor.shape = love.physics.newRectangleShape(love.graphics.getWidth(),1)
	floor.fixture = love.physics.newFixture(floor.body,floor.shape)
	
	local ceiling = {}
	ceiling.body = love.physics.newBody(physicsWorld,love.graphics.getWidth()/2,0)
	ceiling.shape = love.physics.newRectangleShape(love.graphics.getWidth(),1)
	ceiling.fixture = love.physics.newFixture(ceiling.body,ceiling.shape)
	
	local leftSide = {}
	leftSide.body = love.physics.newBody(physicsWorld,0,love.graphics.getHeight()/2)
	leftSide.shape = love.physics.newRectangleShape(1,love.graphics.getHeight())
	leftSide.fixture = love.physics.newFixture(leftSide.body,leftSide.shape)
	
	local rightSide = {}
	rightSide.body = love.physics.newBody(physicsWorld,love.graphics.getWidth(),love.graphics.getHeight()/2)
	rightSide.shape = love.physics.newRectangleShape(1,love.graphics.getHeight())
	rightSide.fixture = love.physics.newFixture(rightSide.body,rightSide.shape)	
end
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: CollisionCallbacks/Contact

Post by pgimeno »

Why don't you show us what you have tried and didn't work, so we can see why it didn't work?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 215 guests