Page 1 of 1

Collison- yes i know more collision help

Posted: Thu Apr 17, 2014 2:31 am
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_

Re: Collison- yes i know more collision help

Posted: Thu Apr 17, 2014 1:25 pm
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.

Re: Collison- yes i know more collision help

Posted: Fri Apr 18, 2014 12:02 am
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

Re: Collison- yes i know more collision help

Posted: Fri Apr 18, 2014 1:08 pm
by Ranguna259
Hmmm.. This looks really wierd, everything looks fine.
Could you post the .love ?

Re: Collison- yes i know more collision help

Posted: Fri Apr 18, 2014 4:01 pm
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