Help with a 2d collision problem - [FIXED]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Uhfgood
Prole
Posts: 35
Joined: Sat Jul 28, 2012 10:04 pm
Location: Oregon, US
Contact:

Help with a 2d collision problem - [FIXED]

Post by Uhfgood » Wed Nov 21, 2012 7:46 pm

I've done tile-based collision before, and have gotten it working many times. I'm actually trying to implement slopes (well tiles with heightmaps), and I've run into some brick walls, so I decided to try a new method. Before I can use the new method to detect slope collisions, I need help with making regular tile collisions with my new method.

The idea is to have a collision point on the right side of the sprite. Then create a line from the collision point to the next "move" or basically the length of the delta in x -- basically x velocity * dt -- so dx = xVelocity * dt; Then to iterate through that line, if there's no collision add that position onto the sprite's position. If there's a collision then set the sprite's position to align with the tile it's colliding with. (sitting up against it). Having it update the position multiple times through a loop using the move (dx) as the length, was intended to stop tunneling. (I've used swept collision before), however it doesn't seem to be colliding at all for some reason.

And now for some code.

Code: Select all

function CPlayer:MoveRight( map, dx, sx, sy )
--
	-- collision point's x value
	local x1 = math.floor( sx );
	-- a line to the next move
	local x2 = math.floor( sx + dx );
	local y = math.floor( sy );
	
	local cellX = math.floor( x1 / map.tileWidth );
	local cellY = math.floor( y / map.tileHeight );
	-- walk the line from x1 to x2
	for loopX = x1, x2 do
		cellX = math.floor( loopX / map.tileWidth );
		local tile = map("Tile Layer 1")(cellX, cellY);
		if ( tile ) then
			tileType = tile.properties.type;
			isBlocked = tile.properties.block_w;
			if( tileType == "border" and isBlocked )then
				self:SetXPos( (cellX * map.tileWidth) - self:GetWidth() );
			else
				-- means we can move
				currX = self:GetXPos();
				-- we only want to add the loop increment value
				self:SetXPos( currX + ( loopX - x1 ) );
			end;
		else
			-- we can also move here
			currX = self:GetXPos();
			-- we only want to add the loop increment value
			self:SetXPos( currX + ( loopX - x1 ) );
		end;
	end;
--
end;

function CPlayer:UpdateHorz( dt, map )
--
	local dx = self.velocity:GetX() * dt;
	if( dx > 0 )then
		xp, yp = self:GetCollisionPointA();
		self:MoveRight( map, dx, xp, yp );
	end;
--
end;
---------------------
I just fixed it, using an 'and' instead of 'or'

User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Help with a 2d collision problem - [FIXED]

Post by rokit boy » Thu Nov 22, 2012 7:37 pm

Thanks, glad I could help. Also, post in "Support and Development"
u wot m8

User avatar
Uhfgood
Prole
Posts: 35
Joined: Sat Jul 28, 2012 10:04 pm
Location: Oregon, US
Contact:

Re: Help with a 2d collision problem - [FIXED]

Post by Uhfgood » Sun Nov 25, 2012 12:09 am

I thought Support and Development was for the development of Love2d itself?

User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Help with a 2d collision problem - [FIXED]

Post by Nixola » Sun Nov 25, 2012 12:11 am

Uhfgood wrote:I thought Support and Development was for the development of Love2d itself?
No, that's to get support in developing with LÖVE
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests