Page 1 of 1

Using variables from another file?

Posted: Wed Jan 13, 2016 5:43 pm
by arr5454
Hey all,

I am trying to make a Breakout clone involving an entity system, however I can't figure out how to check for collision between the ball and the paddle. I currently have a require statement at the top of ball.lua and the lines of code that should cause the ball to bounce off of the paddle commented out. When I run this, it causes the ball to spawn at the correct location (x and y values passed in the ents.Create() function in main.lua) but the ball does not bounce off the paddle.

When the require statement and the lines are uncommented and run, the ball will only spawn in the top right corner of the screen and will collide with what looks like an invisible "paddle" at the position where the x and y coordinates are passed into ents.Create() which should be the spawn position of the ball.

How would I be able to get the x and y position of the paddle from the paddle.lua file to check for collisions in the ball.lua file without using global variables?

Re: Using variables from another file?

Posted: Mon Jan 18, 2016 5:20 pm
by alberto_lara
You can access the bounds of the paddle just like this:

Code: Select all

function love.load()
    local sWidth = love.graphics.getWidth()
    local sHeight = love.graphics.getHeight()

    ents:Startup()
    local ballEnt = ents.Create("ball", 100, 100)
    local paddleEnt= ents.Create("paddle", sWidth/2 - 64, sHeight - 4)
    print(paddleEnt.x, paddleEnt.y, paddleEnt.width, paddleEnt.height)
end
Then you need to detect a circle <> rectangle collision, here's a good approach:
https://gist.github.com/vonWolfehaus/5023015