Collison- yes i know more collision help

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
SSheep_
Prole
Posts: 3
Joined: Thu Apr 17, 2014 2:14 am
Location: Canada :D

Collison- yes i know more collision help

Post by SSheep_ »

Hello their!
Now I'm new to programming i've been at it for like 3 days now.I've been making simple games like adventure games were you click buttons for answers like the walking dead :cool: ! I want to start more compacted things but for that i need to learn how to make collision or a hitbox. I've looked for help but all were badly explained and i didn't get the grasp on it. So if anyone has good easy tutorials that can give that will be awesome! Also i could use a library if i have to but people say i should get the grasp of making collision my self. :o

thanks for the help
-SSheep_
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Collison- yes i know more collision help

Post by Ranguna259 »

I usualy recomend to look into [wiki]love.physics[/wiki], it's not as hard as the page says it is but people here tend to say not use it as it's overpowerd if you are thinking on doing simple things but hey I've been using it for a long time and I had not problems with it.
Here's a sample "hitbox", more like a button:

Code: Select all

if x > box.x and x < box.x+box.width and y > box.y and y < box.y+box.height then
  -- the (x,y) coordinates are inside the box
end
EDIT: Also the thing about collision is not how you detect if two bodies are colliding, it's the response you get after they collide, that's the hard part because you never know if the body is hitting from the right or from the left or from anywhere, in this case all you can do is to guess but by guessing you'll probably get lots of bugs like unwanted body teleportations. Best of luck :)

EDIT2: Here's circle to point collision detection
The dist function:

Code: Select all

function dist(x1,y1,x2,y2)
  return (math.sqrt((x1-x2)^2+(y1-y2)^2)))
end
The collision:

Code: Select all

if dist(circle.x,circle.y,x,y) <= circle.radius then
  -- point is inside the circle
end
Circle to circle collision detection:

Code: Select all

if dist(circle1.x,circle1.y,circle2.x,circle2.y) < cirlce1.radius+circle2.radius then
  -- the two circle are colliding
end
Rectangle to rectangle:

Code: Select all

if obj1.x+obj1.width < obj2.x or obj1.x > obj2.x+obj2.width or obj1.y+obj1.height < obj2.y or obj1.y > obj2.y+obj2.height then
  --the two rectangles are colliding
end
All those are the easy ones then it start to get complicated when you collide special convex shapes.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
SSheep_
Prole
Posts: 3
Joined: Thu Apr 17, 2014 2:14 am
Location: Canada :D

Re: Collison- yes i know more collision help

Post by SSheep_ »

Hi thanks for the replay very easy to understand but i got a error when trying to use it

heres my code

Code: Select all

require "player"

function love.load()

	love.graphics.setBackgroundColor(255,255,255)

	gameState = "playing"

	boxX = 100
	boxY = 100
	boxWidth = 32
	boxHeight = 32
end

function love.update(dt)
	if gameState == "playing" then
	    playerMove(dt)
	    if player.x > boxX and player.x < boxX + boxWidth and player.y > boxY and player.y < boxY + boxHeight then
	    	love.graphics.setColor(255,20,0)
  			love.graphics.print("test", 200,200)
		end
	end

end

function love.draw()
	if gameState == "playing" then
		playerDraw()
		love.graphics.setColor(255,20,0)
		love.graphics.rectangle("fill", boxX, boxY, boxWidth, boxHeight)
	end
end
and my error is

syntax error: main.lua:20 '=' expected near 'love'

i tried to see if i can fix it but i can't find the error
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Collison- yes i know more collision help

Post by Ranguna259 »

Hmmm.. This looks really wierd, everything looks fine.
Could you post the .love ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
SSheep_
Prole
Posts: 3
Joined: Thu Apr 17, 2014 2:14 am
Location: Canada :D

Re: Collison- yes i know more collision help

Post by SSheep_ »

Ranguna259 wrote:Hmmm.. This looks really wierd, everything looks fine.
Could you post the .love ?
I acutely fixed it i just copied the code and past it again and it worked lol

thanks for the help :D
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests