Collision detection (via distance vector)

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Collision detection (via distance vector)

Post by LuMo »

EDIT:
solved now, started the code all over, its more generally now (no special function for every direction) added a array with direction-data
NOTE: had to increase the radius... cause it was able to run through block

something i still do NOT understand...
when i check the radius to be
<=8 i can walk through
<9 i can not walk through
thought smaller&eqal 8 would be -??..8
and smaller than 9 would be -??..8 too

here i still let the original question remaining due some ppl may have a look on the idea itself (even if its something basic :D )

hello, i try to detect collisions via distance vector,
i'm working with a 16x16px tile-based "engine"

so my aim is:
get the middle of my player1 (got that by player.p1.posx, player.p1.posy)
fine so we got the coordinates for my player1
further we have a battlefield
when moving up i have to check TWO points

the lower right and the lower left edge of the tile above the player (where he wants to go to!)

so i need to get the tilex and tiley# of the player, where he is standing
also got that by player.p1.tilex, playerp1.tiley
we are getting somewhere ;)

further i get the tile above the player by: Map[player.p1.tilex][player.p1.tiley-1]
note: -1 cause we want the one above him!

in keyboard controls i use this code:

Code: Select all

	pad = Controls.read&#40;&#41;
	p1_moves = false
	if pad&#58;up&#40;&#41; then
		if canMove&#40;up&#41; then
			--movement-code here
			end
		end
		--more code for other directions....
here my function to get the collision:
take 2 points and see if the distance is smaller than 4 (radius)

Code: Select all

function collide&#40;x1,y1,x2,y2&#41;
		dx=&#40;x2 - x1&#41;
		dy=&#40;y2 - y1&#41;
		distance = math.sqrt&#40; dx*dx + dy*dy &#41;	
		if distance<4 then
			return true
		else
			return false
		end
end
further i use canMove(direction)
--note: direction is an integer value 1..4 defined as global variable (for easier programming, than remembering was 1 up or was it 0)

Code: Select all

function canMove&#40;direction&#41;

--check the distance between two points...
-- x1,y1 & x2,y2
--if dx = &#40;x2 - x1&#41; and dy = &#40;y2 - y1&#41; then
--distance = square root of &#40; dx * dx + dy * dy &#41;
--This assumes x1,y1 is the centre of bomberman

--check 2 points for collision &#40;of ONE tile&#41; <-- SIMPLYFIED, was 4 once!
--x;x+16,y;y+16

--set bombermans center&#58;
	x1=player.p1.posx
	y1=player.p1.posy
	x2=player.p1.tilex --check some variations
	y2=player.p1.tiley --check some variations
	--check the DOWN direction
	if direction==down then
		--check the upper 2 points
		--point1 upper left one
		if collide&#40;x1,y1,x2,y2&#41; then
			return false
		--point2 upper right one &#40;+16 cause i have 16x16 sized tiles&#41;
		elseif collide&#40;x1,y1,x2+16,y2&#41; then
			return false
		else
			return true			
		end	
	--other directions follow here
end
my problem now is, that my player can still pass the "solid" stones...
but i doo not want to increase the radius cause otherwise i have to
be exactly on the pixel to pass the block...

hope you get what i mean&try

greets LuMo
Post Reply