Simple Collision Detection

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
Jhawsh
Prole
Posts: 4
Joined: Mon Oct 15, 2012 1:11 pm
Contact:

Simple Collision Detection

Post by Jhawsh »

I am trying to make a basic game. Just a box you can control with "wsad" and you can go outside the window.
My collision detector only stops when it outside the box and does not allow it to move afterwards, i am stuck and cant find out what to do.
Here is my code.

Code: Select all

function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)

  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end

function MapCollision()

	
end	

function InMap()
	return CheckCollision(0, 0, love.graphics.getWidth(), love.graphics.getHeight(), playerx, playery, 10,10)
end

function love.load()
	waterimage = love.graphics.newImage("images/water.png")
	playerimage = love.graphics.newImage("images/player.png")
	screenWidth = love.graphics.getWidth()
	screenHeight = love.graphics.getHeight()
	playerx = 10
	playery = 10
end

function love.update(dt)
	
	if love.keyboard.isDown("w") and InMap() then
			playery = playery - 100 * dt
	end

		if love.keyboard.isDown("s") and InMap() then
				playery = playery + 100 * dt
			end

				if love.keyboard.isDown("d") and InMap() then
					playerx = playerx + 100 * dt
				end
	
						if love.keyboard.isDown("a") and InMap() then
							playerx = playerx - 100 * dt
						end


function love.draw()
	love.graphics.setBackgroundColor( 255, 255, 255 )
	love.graphics.setColor( 0, 0, 0, 255 )
	love.graphics.rectangle("fill", playerx, playery, 10, 10 )
end
end
I have attached the .zip of the files
Attachments
Game.zip
Game ZIP
(12.19 KiB) Downloaded 194 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Simple Collision Detection

Post by kikito »

If the code in your post is the same as the code in your .love, I recommend you take the time to indent it properly. You may find surprises. For example: is not immediately apparent because of the spacing, but in that code, love.draw is being redefined every time love.update is invoked.
When I write def I mean function.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Simple Collision Detection

Post by Helvecta »

Wait, wait wait! Can you rephrase that? I'm confused about how you want the box to behave. :oops:
"Bump." -CMFIend420
Jhawsh
Prole
Posts: 4
Joined: Mon Oct 15, 2012 1:11 pm
Contact:

Re: Simple Collision Detection

Post by Jhawsh »

Helvecta wrote:Wait, wait wait! Can you rephrase that? I'm confused about how you want the box to behave. :oops:
If they try and go outside the window it stops.
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: Simple Collision Detection

Post by Larsii30 »

(*Delete old post because of bullshit.*)

Maybe try something like this:

Code: Select all


lk = love.keyboard

if lk.isDown( 'up' ) and player.y > 0 then player.y = player.y - player.s * dt
elseif lk.isDown( 'down' ) and player.y < screen.height - player.h then player.y = player.y + player.s * dt
end
	
if lk.isDown( 'left' ) and player.x > 0 then player.x = player.x - player.s * dt
elseif lk.isDown( 'right' ) and player.x < screen.width - player.w then player.x = player.x + player.s * dt
end

Knaimhe
Prole
Posts: 34
Joined: Mon Jan 23, 2012 7:23 pm

Re: Simple Collision Detection

Post by Knaimhe »

I've been working with the Box2D engine in Love2D for some time now.

An easy fix for you would to be to create a set of walls (using edgeshape or chainshape). If you plan to have other objects and only want to stop the box, you could set the box's category and the walls' mask to collide only with each other.

https://love2d.org/wiki/ChainShape
https://love2d.org/wiki/EdgeShape
https://love2d.org/wiki/Fixture:setMask
https://love2d.org/wiki/Fixture:setCategory

The reason you lose control of the box is because it has been put to sleep or set to inactive. I'm not too sure which; active/inactive was recently introduced and I don't really grasp the distinction, but I think it goes something along the lines of this:
If a body comes to rest, it goes to sleep if sleeping is allowed. Sleeping objects are easier to simulate.
If a body exits the world, it becomes inactive, even if it is attached to a joint. Inactive objects cannot be directly manipulated with force/torque/impulse. They do not cause collisions, either.

You can try disallowing sleeping in your world, but I don't think that would do it.
https://love2d.org/wiki/World:setAllowSleeping
You can also try resetting the box to active. Whenever an object exits the world's boundaries, it becomes inactive. If you only need to reset this one object to active, you can do this:
https://love2d.org/wiki/Body:setActive
for every frame. Just stick this:
Body:setActive(true)
into a function which is called every frame (such as update).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 4 guests