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 

Image collision checking

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



Joined: 27 Dec 2005
Posts: 4

PostPosted: Tue Dec 27, 2005 9:05 pm    Post subject: Image collision checking Reply with quote

I'm trying to get collision checking for my working.

I tried to do it this way, but it just went REALLY slow, and I'm not sure if it even worked. The way I tried to do it probably isn't the best way either.
Code:

   for y = y1, y1+img1:height() do
      for x = x1, x1+img1:width() do
         for ty = y2, y2+img2:height() do
            for tx = x2, x2+img2:width() do
               tcolPix = testimg:pixel( x, y );
               tcolTPix = testimg:pixel( tx, ty );
               colPix = tcolPix:colors();
               colTPix = tcolTPix:colors();
               
               if ( colPix == colTPix ) then
                  return true;
               end
            end
         end
      end
   end


Does anyone else know of a way I could do it? The way the images in my game arn't really detailed or anything, so I could just do it like bounding boxes, but I don't know how that would work.

The old way I had it was like this:
Code:

   if ( (shipX >= platformX) and (shipX+imgShip:width() <= platformX+imgPlatform:width()) ) then
      if ( (shipY+imgShip:height() >= platformY-1) and (shipY+imgShip:height() <= platformY) ) then
         if ( shipSpeed < 0.6 ) then
            -- Do stuff
         end
      end
   end

But that only worked if the ship was exactly inside the platform area, and not if it was half way in. (It's also only uses just a small section of the Y to check because it didn't work properly if I used the whole image.

Thanks.
Back to top
View user's profile Send private message MSN Messenger
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Tue Dec 27, 2005 10:38 pm    Post subject: Re: Image collision checking Reply with quote

Knifa wrote:
I'm trying to get collision checking for my working.

I tried to do it this way, but it just went REALLY slow, and I'm not sure if it even worked. The way I tried to do it probably isn't the best way either.
Code:

   for y = y1, y1+img1:height() do
      for x = x1, x1+img1:width() do
         for ty = y2, y2+img2:height() do
            for tx = x2, x2+img2:width() do
               tcolPix = testimg:pixel( x, y );
               tcolTPix = testimg:pixel( tx, ty );
               colPix = tcolPix:colors();
               colTPix = tcolTPix:colors();
               
               if ( colPix == colTPix ) then
                  return true;
               end
            end
         end
      end
   end


Does anyone else know of a way I could do it? The way the images in my game arn't really detailed or anything, so I could just do it like bounding boxes, but I don't know how that would work.

The old way I had it was like this:
Code:

   if ( (shipX >= platformX) and (shipX+imgShip:width() <= platformX+imgPlatform:width()) ) then
      if ( (shipY+imgShip:height() >= platformY-1) and (shipY+imgShip:height() <= platformY) ) then
         if ( shipSpeed < 0.6 ) then
            -- Do stuff
         end
      end
   end

But that only worked if the ship was exactly inside the platform area, and not if it was half way in. (It's also only uses just a small section of the Y to check because it didn't work properly if I used the whole image.

Thanks.


Personally I would place an array arranging all the images.

Like
ShipArray = { }
table.insert(ShipArray, x,y,w,h, Image.load( filename ))

That will allow you to setup bounding box's for each image as well as place the images however you wish. As long as you place each element drawn in a loop.

Another trick to speed up refresh time is to create a preimage not including your (moving image) and have everything refresh over time.

This should allow your game to be less choppy and hopefully have a little cleaner code.

If you have any other questions let me know.

The rest is up to you..
Back to top
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Wed Dec 28, 2005 12:02 am    Post subject: Reply with quote

w, h, is not even required, cause you can get width and height from the loaded image itself...
Code:

image:width()
image:height()

moe functions @ the wiki

greets lumo
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Wed Dec 28, 2005 12:31 am    Post subject: Reply with quote

w/h is to allow for bigger immages to be used as a smaller/colision based system. So transparancys and image:width() etc.. wont make it look bad.

(just thinking ahead for you!)
Back to top
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Wed Dec 28, 2005 12:57 am    Post subject: Reply with quote

possibly use distance vector based calculation (faster?)
and you do not need to check that much...
(but ... distance would be calced as circle)

greets
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
Dr. Vegetable



Joined: 14 Nov 2005
Posts: 171
Location: Boston, Massachusetts

PostPosted: Wed Dec 28, 2005 1:20 am    Post subject: Reply with quote

knifa wrote:

The old way I had it was like this:
Code:

   if ( (shipX >= platformX) and (shipX+imgShip:width() <= platformX+imgPlatform:width()) ) then
      if ( (shipY+imgShip:height() >= platformY-1) and (shipY+imgShip:height() <= platformY) ) then
         if ( shipSpeed < 0.6 ) then
            -- Do stuff
         end
      end
   end

But that only worked if the ship was exactly inside the platform area, and not if it was half way in. (It's also only uses just a small section of the Y to check because it didn't work properly if I used the whole image.

It looks like you were on the right track with your original code. The trick to collision-detecting two rectangles is to compare opposite sides, like this:
Code:

   if ( (shipX+imgShip:width() >= platformX) and (shipX <= platformX+imgPlatform:width()) ) then
      if ( (shipY+imgShip:height() >= platformY) and (shipY <= platformY+imgPlatform:height()) ) then
         if ( shipSpeed < 0.6 ) then
            -- Do stuff
         end
      end
   end

Conceptually, what this does is to compare the left side of one rectangle with the right side of the other, and v/v. If A's right side is to the right of B's left side, and A's left side is to the left of B's right side, and if A's top is above B's bottom and A's bottom is below B's top, then A and B at least partially overlap.

Sorry if that explanation is more "Dr. Seuss" than "Dr. Vegetable..."
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Knifa



Joined: 27 Dec 2005
Posts: 4

PostPosted: Wed Dec 28, 2005 2:16 am    Post subject: Reply with quote

[quote="Dr. Vegetable"]
knifa wrote:

Code:

   if ( (shipX+imgShip:width() >= platformX) and (shipX <= platformX+imgPlatform:width()) ) then
      if ( (shipY+imgShip:height() >= platformY) and (shipY <= platformY+imgPlatform:height()) ) then
         if ( shipSpeed < 0.6 ) then
            -- Do stuff
         end
      end
   end

Conceptually, what this does is to compare the left side of one rectangle with the right side of the other, and v/v. If A's right side is to the right of B's left side, and A's left side is to the left of B's right side, and if A's top is above B's bottom and A's bottom is below B's top, then A and B at least partially overlap.

Sorry if that explanation is more "Dr. Seuss" than "Dr. Vegetable..."


That works, thanks alot. I don't entirely understand how it works, but I'll get it eventually.[/i]
Back to top
View user's profile Send private message MSN Messenger
thepelkus



Joined: 05 Jan 2006
Posts: 1

PostPosted: Thu Jan 05, 2006 2:21 am    Post subject: Reply with quote

The other way that I've always liked, just a restatement of the good Doctor's code for the sake of legibility, is:

Code:
collide=1

     if shipX>platformX+imgPlatform:width() then collide=nil
else if platformX>shipX+imgShip:width()     then collide=nil
else if shipY>platformY+imgPlatform:height  then collide=nil
else if platformY>shipY+imgShip:height()    then collide=nil

if collide then
-- do something


Again, just checking to see if either element in the comparison is clearly to one side of the other, setting a collide flag to nil if it's ever the case. Note that, as a lua novice, I'm not sure about the syntax, but that should be close if not right.

Additionally, if you did want pixel perfect collision after that, you could run your pixel test code only on the intersection of the two rectangles.
Back to top
View user's profile Send private message
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