forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Collision detection (via distance vector)

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Mon Sep 05, 2005 9:31 pm    Post subject: Collision detection (via distance vector) Reply with quote

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:
   pad = Controls.read()
   p1_moves = false
   if pad:up() then
      if canMove(up) 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:

function collide(x1,y1,x2,y2)
      dx=(x2 - x1)
      dy=(y2 - y1)
      distance = math.sqrt( dx*dx + dy*dy )   
      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:

function canMove(direction)

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

--check 2 points for collision (of ONE tile) <-- SIMPLYFIED, was 4 once!
--x;x+16,y;y+16

--set bombermans center:
   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(x1,y1,x2,y2) then
         return false
      --point2 upper right one (+16 cause i have 16x16 sized tiles)
      elseif collide(x1,y1,x2+16,y2) 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
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group