[solved]Collision response: the right direction

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
M3G0
Prole
Posts: 2
Joined: Tue Aug 14, 2018 12:03 pm

[solved]Collision response: the right direction

Post by M3G0 »

Hey,

I'm new to this forum and I recently picked up Lua and Love2D. After a few tutorials, I started to feel confident and make my very first game.

Currently I have been making a AABB collision and response program that will correct the player depending on the moving object direction.

Code: Select all

function collisions:update()
	-- checks for every object on scene
	for i = 1,#colliders do
		--checks for every tile (solid) on scene
		for j = 1,#platforms do
		
			object = colliders[i]
			platform = platforms[j]
			 
			collision = ( object.x + object.width > platform.x and object.x < platform.x + platform.width and object.y + object.height > platform.y and object.y < platform.y + platform.height)
			
			if collision == true then
				print ("hey")
				if object.dirX > 0 then 
				object.x = platform.x - object.width -1
				print("r")
				end
				if object.dirX < 0 then 
				object.x = platform.x + platform.width +1
				print ("l")
				end
				if object.dirY < 0 then 
				object.y = platform.y + platform.height +1
				print ("u")
				end
				if object.dirY > 0 then 
				object.y = platform.y - object.height -1
				print ("d")
				end
			end
			
			--gravity
			
		end
	end
end
The problem however is that as seen on the attachment. The blue box (moving object) is attempting to collide with the static black box. Because it is moving diagonally, it sets the variables dirX and dirY to be true thus sending the blue box to the orange position rather than the desired yellow position. Any ideas how to fix this?

Edit 10/12/2018:
I simply tagged objects if they are in the x axis of moving object or y axis of moving object
Attachments
Pic.png
Pic.png (7.08 KiB) Viewed 2075 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 6 guests