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 

cpplibs: howto use Mask for pixelprecise collision

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



Joined: 20 Jan 2007
Posts: 4

PostPosted: Thu Feb 15, 2007 8:07 am    Post subject: cpplibs: howto use Mask for pixelprecise collision Reply with quote

I just started with the example from cpplibs. I really like this lib, because it's pure c++ and easy to use. Now I want to detect collisions on pixel level, but I wonder how the Mask class is intended to use.

Here's my first plan how to use the Mask class, but I don't think this is the right way:
1) find out overlapping area of 2 sprites (Images)
2) Create two Mask objects of the 2 Images with the overlapping area
3) call collide

But this way is not efficient because I have to create every Frame a new Mask object. Wouldn't it be better if the Mask is just created once, and then call collide every frame with the overlapping area of the two Mask objects?

Or does someone know a better way for testing if pixel of two images overlap?
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Thu Feb 15, 2007 11:58 am    Post subject: Reply with quote

this is not precise as some sprites may contain
alpha channel set in some areas surrounding it
as 0 example would be a heart shaped object
course all sprites are squarish in nature so you would
be detecting mask collisions around this box and not
by the borders of the heart shaped object
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
stelzbock



Joined: 20 Jan 2007
Posts: 4

PostPosted: Fri Feb 16, 2007 6:24 am    Post subject: Reply with quote

ok, then you can set just the mask bit of opaque pixels. I think that's not the problem.
Back to top
View user's profile Send private message
fraca7



Joined: 03 Oct 2005
Posts: 21
Location: Paris

PostPosted: Sun Feb 18, 2007 11:26 pm    Post subject: Reply with quote

Hello. The 'Mask' class was pretty useless until now. I just upgraded it; you can grab the latest sources through SVN at this URL:

svn://fraca7.homeunix.net/python/trunk/cpplibs/

You instantiate the mask passing it an Image and (x, y, w, h) coordinates. It will then build a bitset of pixels which alpha value is greater than 'threshold' in the specified rectangle of the image. Two new optional parameters, 'x' and 'y', specify the position of the sprite. You can update this with the move(x, y) method.

Calling collide() returns the number of bits in common in the two masks, taking into account their respective position. So you just have to call move() to update the mask according to the sprite's current position and then collide() to see if the two sprites collide.

I didn't test this though...

HTH
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 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