Help PF catch up with GD!
Thierry, you of course know by my posts in the GD forum that I've become, shall we say, a tad bit disallusioned with that library...
Well, I spent a couple of hours trying to convert my current game over to PF. It went fairly smoothly, the little bit I did anyway, and I think the rest would go fairly easily and not take more than a couple of days.
As you stated in a recent thread here, PF does lag GD in terms of functionality. Not by a great deal, but it does. You also said you are going to be working to address that situation, which is an excellent thing to hear.
So, I thought maybe a thread here with the functionality we'd like to see added might be in order. I will, as before, contribute wherever I can and have the time to, but I'll be blunt: I stopped developing my own library over a year ago because you are more capable than I, and I want to spend my time on my games, not an underlying library. So, from that perspective, I really am counting you to expand PF, assuming I'm going to make the jump back (I understand you could care less whether I went with PF or GD, I'm just saying

)
Anyway...
The only thing that PF is missing that will cause me some angst in converting my current project over is the lack of an Intersect function. GD has it, and it's how I do all my colission detection. I think some form of colission detection is the one thing PF is missing that it probably needs. It's the only remaining "basic" functionality that a game needs that isn't there.
Sure, I can roll my own, and will have to it PF doesn't get it, but I'd prefer writing more of my game than having to bother with such a relatively low-level thing.
I can do without all the tints and colorizes and other effects that GD offers (although it's cool that they are there, PF offering shaders compensates nicely). You already know I'm high on DX support as well. But collision detection, I think that's something worth putting some time and effort into in my opinion.
I know what you probably going to say... there's so many different ways to do it, doing it in a general way is either very difficult or will yield less-than-optimal results. And you'd be right to say it. But, the GD Intersect functions works pretty well performance-wise, is simple to use, and I think will do the job 95% of the time for most people. If you could duplicate it's functionality, I believe it would be a big boost to PF's usefulness.
Anyone else have suggestions along these lines?
fzammetti wrote:
The only thing that PF is missing that will cause me some angst in converting my current project over is the lack of an Intersect function. GD has it, and it's how I do all my colission detection. I think some form of colission detection is the one thing PF is missing that it probably needs. It's the only remaining "basic" functionality that a game needs that isn't there.
Are you referring to the GapiMaskSurface Class or the CRect Intersect function?
I personally use the CRect Intersect function for collision detection because both PF and GD support it.
Pam
Huh? Did I miss something? PF supports that intersect function?
I was in fact referring to the CGapiSurface::Intersect function.
Maybe I completely missed something in PF, does it already do this? I do need it to be based on surfaces, is that the case?
Damn, PM logged me out... That was me, in case you couldn't guess (but of course it was obvious, so this was a stupid post... but THAT was obvious too, so... ah, never mind)
OK, I understand now. PF doesn't have a pixel-by-pixel Intersect function between two surfaces.
Pam
PF can tell when 2 rectangles intersect. Pixel perfect intersects with masks can be coded easily on top of this.
BTW it's very wrong to imply that PF is somehow lacking in comparison to GD. I have twice looked at both in detail & see no reason to give up using PF. If MS Smartphones had enough ram & fast processor to run my games I would consider porting PF however having had a play with a real Orange SPV E100 recently I decided not to buy one as it's a pretty poor platform. (so I bought a cheap Mondo instead to have a PocketPC phone just for fun)
Your right, it's not an incredibly difficult thing to implement... My point though was that it's something I don't have to do myself when using GD, and it saves me some measure of time therefore. I'd like to see PF come out with it so that I can continue to save that time, should I switch back.
Remember that a little over a year ago I was pretty far along on my own graphics library. But, for a number of reasons, I decided that using someone else' was a better option. If for no other reason it allows me to focus on my projects, not the low-level code to make it work, same reason anyone chooses any library at all. Therefore, anything taht can be added to the library that I personally don't have to put time into is a welcome thing to me.
About how PF compares to GD, keep in mind that I love PF and see no real problem with it.
What convinced me to switch to GD was first and foremost DirectX support. I wanted the capability to simultaneously release for desktop and PocketPC, and GDI with PF just doesn't cut it in most cases. That more than anything got me to switch.
At the time I thought there was a major problem with image loading in PF, but I've since learned that's not the case. I think GD DOES have better image loaders, but that's not a big problem.
The collision detection built into GD was also a draw, but not a huge one.
As I recall, those were the points that got me to go with GD for my current project. That and the fact that, at the time, I was the only one supporting PF at all, and I don't frankly think I'm good enough to be doing that anyway. GD was supported and seemed likely to continue to be evolved by someone other than me, and that was also a factor in my decision.
But, fast-forward 4-5 months...
Thierry is back on the scene, so the support factor isn't a problem any more (at least not to the same degree).
Thierry has plans on doing a DirectX driver, so that factor is or at least will be nullified.
Thierry is (I think has?) rewritten the image loaders, so that GD advantage has been nullified.
So, in my list of points, colission detection is the only remaining point in GD's favor. Is it a huge one? No, least important of the bunch. But I see it as a valid suggestion to make, and one small area that GD has an advantage that can easily be matched or exceeded.
But, also consider that GD now supports Symbian, and is going to support Palm. Those are also considerations to me. The more platforms I can release on without substantially differing code bases, the better.
With all the licensing shennanigans over in GD world now, I may very well be willing to give up that aspect of it though.
Isn't collision detection as simple as going through each pixel in the image masks and seeing if that pixel has a set bit in each mask? So you have to have two masks, and two positions for that mask. You return the pixel intersecting, or none if no collision.
We could code that in minutes and put it in PocketFrog. No problem.
I don't use that type of collision detection myself, but I could see how it might be useful to others.
I'm just using the built in InRect function for collision detection

A main concern I have is, can I build with PocketFrog for PC? At least for development and testing? How much effort is involved in doing this?
My app currently uses GapiDraw and takes about 25ms on Pocket PC and 5ms on PC. So I have plenty of ms left to spare on the PC build.
I guess I should just download PocketFrog and check out the samples and tutorials, but a quick answer to this would be kind.
mlepage wrote:
A main concern I have is, can I build with PocketFrog for PC? At least for development and testing? How much effort is involved in doing this?
I guess I should just download PocketFrog and check out the samples and tutorials, but a quick answer to this would be kind.
The quick answer is that PocketFrog's first goal was to allow development on the desktop. (I didn't have a CF2 network card back then.) So yeah, it does support it.
in reply to mlepage PocketFrog works fine on PC. The PC app runs much faster than the PocketPC one unless you include a frame rate limiter.
When building for both you need to understand the _t stuff ( TCHARS, TEXT("") etc and there are changes to file paths to build in but it's really easy & saves oodles of time over the emulator.
I'm happy to help if you don't already know this stuff & it won;t take long to get set up.
I already use all the _T stuff. I currently build for both PC and PPC using GapiDraw. So given that, how much of a headache to move to PocketFrog (just the PC/PPC issue, not other API issues)?
Is there an analogue to GapiDraw's "minimal" application that I can just start from? I typically build it in VC++6 for PC, and separately in eVC++3 for PPC. There are two projects which include all my source files. I don't use resources.
Last night it took me about two hours to get a small portion of my current GD app switched over to PF. I have the benefit of knowing both, but the bottom line is it shouldn't be that tough.
A quick run-down of what was required:
(1) All the usual environment setup stuff (see the sample apps included with the PF download for that)
(2) Assuming you use the PF framework, you'll beed to change your method names and signatures (really in trivial ways) to conform to PF's way. Not a big deal.
(3) PF's blit function maps almost perfectly to GD's blt, as long as you didn't use any of the options. If it's just a straight blit it's a simple change of names and types.
(4) For other blit types it'll be a bit more work. PF has a separate blt function for rotates and stretches whereas they are options in GD.
(5) As I've posted in this thread, I'm not sure about how to convert Intersects yet if you've used them (which I assume most people do for colission detection). In theory it shouldn't require more than writing your own detection function with a signature that's close to Intersect's and call that instead.
In the end, it comes down to mostly busy work, unless you've used a lot of the "fancier" GD options and such. If you've just done a lot of blts it's not a tough conversion on the whole.
mlepage: Don't let me stop you coding that up for me
The question that comes to mind is performance... How do you go about optimizing that algo? At the least it would take some time to research, unless Johan wants to share the basic concept as implemented in GD with us.
It also would require, as stated here, extra resources: the image masks. GD works without this (just has the requirement that the surfaces have key colors, i.e., transparent portions, set), which makes it sound like it's a pixel-by-pixel comparison as you stated. The docs say it does a simple bounds check first, which is the obvious optimization, but there's got to be more to it than that, or so I would expect.
fzammetti":2d49tqhf]
The question that comes to mind is performance... How do you go about optimizing that algo?
[/quote="fzammetti"]
You use bitmasks. Although I am unsure if this would be faster then using simple pixel comparaisons.
I don't think there is more to it. You check the bounding rectangles then the pixels. It's pretty basic actually.
Ok, well, I may well have a shot at writing this thing tonight. I think what I'm likely to do (open to suggestions)...
(1) Determine the rectangular overlap region of the two surfaces using bounding rectangles, obviously only proceed if there was an overlap.
(2) Determine the region of each surface where the overlap occurs.
(3) Iterate over this region on one surface, comparing each pixel to the corresponding pixel inm the region on the other. If any pixel is found that is not the key color, collision occurred.
Yeah, from an algorithm perspective, unless I left something daunting out, it's pretty simple. I would think it would perform fairly well in most cases too.
The devil is always in the details though.
Well, if I do it and it works well, I'll send it your way Thierry for possible inclusion in the next version.
Well, there are many ways to handle this issue. Going pixel by pixel is one of the slowest :-)
If you are willing to sacrifice a bit more memory per surface you could prepopulate an additional bit array where each bit would correspond to a single pixel with 0 being transparent ( this would only apply to surfaces requiring collision detection.)
This way you could use logical operations to detect overlapping regions 32 pixels at a time.
Just an idea ....
That's an interesting idea... I think I actually like that one better...
It may be the case that doing a comparison of the entire surface against another surface, regardless of bounding rectangles or overlap areas, might wind up being faster (or no slower anyway) and the code I think would be quite a bit easier to write. And if worse comes to worse, adding a bounding rectangle check later is no big deal, and even dealing with the overlap areas after the fact should be trivial, except that the overlap area would have to be at least 32 pixels wide and tall and would have to be an even multiple of 32, lest the code just get gunked up and less optimized anyway.
As you say, I could make it an optional flag whether to potentially involve a surface in a colission or not, even though the memory required would be trivial even for a very large surface (what, 2.4k for a 240x320 surface, give or take a few bytes between friends?)
I'll toy with that for sure.
Actually you would still do bounding rect check cause that is always the fastest way to eliminate a lot of unnecessary processing.
Once you have determined that these surfaces overlap then using logical ops would be most likely the best solution.
Of course it would be a bit more complicated than doing simple pixel by pixel comparison ( cause you would most likely have to do some shifting of bits) but still , it would probably be much faster.
Lookup Rect::Intersect( Rect ) or something like that... The code already does rect/rect intersection.
Something like:
Rect R = rect1.Intersect( rect2 );
if (!R.Empty())
{
// Test the pixels
}
I would say, first code up the algorithm and lock down the API, then worry about performance.
Even better, post the API and docs here that you'd like to see, then we can ponder how to do it.
Key colours are effectively the same thing as a mask. It should be able to use either.
Are we just checking if the two sprites intersect, or are we also looking for the outermost touching point of some kind?
I would say, after checking bounds, just go linearly through the pixels looking for an intersection point. If the point is closest to the edge, keep it.
So one optimization is to start looking at the edges of the image first.
Another optimization is to find an edge of a sprite, then follow that edge using edge detection.
Finally, a recursive area subdivision method might be quick. Like a binary search on in two dimensions on the sprite.
some time ago i was already thinking about how to do a fast per-pixel collision detection between two sprites. the idea i came up with was to use "mipmapped" collision masks.
so you do a comparision between very coarse (small) masks first. if the tests succeeds you go down one level into the section(s) where the intersection did not failn...
this should work quite fast.
(in fact the idea is stolen from hiararchical z-buffers).
i recently started my own graphics (application) framework (called PocketKnife), since pocketfrog did not suit all my needs.
for an allnew - and actively developed - version of pocketfrog i'd be happy to contribute stuff.
e.g. a console like text output (nice for debugging), a macro block (90° steps) rotator (very fast due to good cache using), etc...
we will also release our OpenGL & OpenGL|ES (subset) implementation at the end of the year. maybe (at least as an extension) this would also fit in there...
bye,
DANIEL
Checking for intersecting rects is done so easily, you should definitely do it.
- Code:
// x1,y1---------+ x3,y3---------+
// | | | |
// +---------x2,y2 +---------x4,y4
if((x1 > x4) || (x2 < x3) || (y1 > y4) || (y2 < y3))
// no intersection
return;
If they do intersect, then do the pixel comparisons.
-John
PF already has built-in rectangle intersect functionality. You can also check for a point inside a rectangle.
The main difficulty is to traverse the pixels in the correct pitch alignment (so you don't trash the cache). That's the main reason it's built into GapiDraw. There are also some tricks you can do for fast 32-bit pixel checks so you can check two pixels simultaneously...
Johan wrote:
The main difficulty is to traverse the pixels in the correct pitch alignment (so you don't trash the cache). That's the main reason it's built into GapiDraw. There are also some tricks you can do for fast 32-bit pixel checks so you can check two pixels simultaneously...
What do you mean by "correct pitch alignment"? I am not sure I understand.
What kind of trick can you use to check two pixels at once? I am really curious about this.
Hi Thierry,
What do you mean by "correct pitch alignment"? I am not sure I understand.
I just meant the usual stuff that you don't always use "for each row, check all pixels in that row". Basic pixel translation that you use in PF. That however won't be an issue with PocketHAL..
What kind of trick can you use to check two pixels at once? I am really curious about this.
I simply read 32-bit, check the masks, and depending on the masks a) do nothing, b) check lower words - shift - and if needed check high word, or c) check high word
Here are my thoughts.
Mipmap-like masks might be interesting. That's another play on area subdivision I suggested.
Preprocessing (e.g. preparing bit masks based on key colour) is probably a good idea. If you worry about overhead, only do it for flagged surfaces.
Comparing multiple bits at a time (with bit AND) is something I also thought of. And as Johan pointed out, you can check one mask first before you even go to the other mask (it might be all zeros).
I guess data alignment is important. Maybe always check on 32-bit boundaries?
I don't know if it would be even quicker to check using __int64.
As for cache stuff, maybe make sure you traverse the proper rows and columns.
For unrolling loops, check out a coding construct called "Duff's Device." Search for it, it will rock your world.
Here's the pixel-perfect collision code: (Modified after having tested it.)
Note that DisplayDevice *pDisplay = GetDisplay();
- Code:
bool Collision(Surface *s1, Point p1, Surface *s2, Point p2)
{
return Collision(s1, p1.x, p1.y, s2, p2.x, p2.y);
}
bool Collision(Surface *s1, int x1, int y1, Surface *s2, int x3, int y3)
{
int x2 = x1 + s1->GetWidth() - 1;
int y2 = y1 + s1->GetHeight() - 1;
int x4 = x3 + s2->GetWidth() - 1;
int y4 = y3 + s2->GetHeight() - 1;
// Rule out non-collisions immediately
if((x1 > x4) || (x2 < x3) || (y1 > y4) || (y2 < y3))
return false;
// Create the rasterizers
Rasterizer *r1 = pDisplay->CreateRasterizer(s1);
Rasterizer *r2 = pDisplay->CreateRasterizer(s2);
// Get the color masks
Pixel t1 = s1->GetColorMask();
Pixel t2 = s2->GetColorMask();
// Determine the x,y points of the collision rectangle
int x5 = (x1 > x3) ? x1 : x3;
int y5 = (y1 > y3) ? y1 : y3;
int x6 = (x2 < x4) ? x2 : x4;
int y6 = (y2 < y4) ? y2 : y4;
// Test the pixels
for(int x = x5; x <= x6; x++)
for(int y = y5; y <= y6; y++)
if((r1->GetPixel(x-x1, y-y1) != t1) && (r2->GetPixel(x-x3, y-y3) != t2))
return true;
return false;
}
-John
[edit]Updated the code[/edit]
If you are serious about working on this, I suggest you do the following...
(Sorry, I just have a preference for effective process. This is just a suggestion.)
Create a simple PF app that has this function with the API you want, and uses it. The app will also display a performance benchmark.
Then, once you have the first implementation, you can record the benchmark. As you try different implementations, you can compare the benchmark.
This will ensure that everyone is working from the same page. We will all be working on the same function in the same app. And even if our benchmarks differ from computer to computer, we can at least compare implementations on our own computer, and one person can volunteer to do canonical benchmarks on their computer.
If you post a sample app I can easily compile (I've never used PF), I will write a collision implementation for it. For fun. :-)
I'll make a small app later that uses this collision function so we can see how it performs. Should be easy enough to do with 2 gifs and some simple text.
-John
depending on the sprites, using an RLE based collision test could even be a lot faster in many cases. of course this is more complicated code than simply running through the pixels.
i still like my idea of having a hierarchical test...
just my thoughts,
DANIEL
presto:
On some devices, changing
for(int x = x5; x <= x6; x++)
for(int y = y5; y <= y6; y++)
into
for(int y = y5; y <= y6; y++)
for(int x = x5; x <= x6; x++)
would result in at least 3x performance increase..
Also, I'm pretty sure GetPixel uses a multiplication, so with two GetPixel you get two multiplications for each pixel where you actually should be able to use none...
would result in at least 3x performance increase..
well, that also depends on the size of the sprite. if it fits nicely into the cache the difference won't be that significant.
but generally you are right. when i found out that the swizzle/unswizzle routines of PocketFrog where the performance problems i rewrote them to use macroblocks (say 32x32 pixels sized) which gave a huge speedup...
DANIEL
Thanks for the tip, Johan.
I almost wrote it that way, but since the x and y are being passed to the GetPixel() function, I didn't think it would make a difference to performance. Unfortunately I can't test it here at work.
Another thing I considered was checking the midpoint of the collision before going into the loop:
- Code:
xc = (x5 + x6) >> 1;
yc = (y5 + y6) >> 1;
if((s1->GetPixel(xc-x1, yc-y1) != tColor) && (s2->GetPixel(xc-x3, yc-y3) != tColor))
return true;
Because if there IS a collision, that's the most likely location for it to occur. But, if there's not a collision, it's a waste of memory cycles. (because the loop would be checking that spot too)
So, since it would only reach that point IF the rectangles were intersecting, would it increase performance? I'm not sure yet. It's probably worth trying.
For anyone who wandered what the "tColor" parameter is, that's the "transparent color", which means all collidable surfaces need the same color mask. I know each surface can have its own color mask, but I wasn't sure how to retrieve the value of the mask. GetColorMask()? If there's a way to do that, the tColor parameter could be eliminated altogether, and it wouldn't matter if the surfaces had differing color masks.
Later,
-John
Hey Thierry, don't look now, but I think people are contributing here!
Presto, I'm about 99% sure GetColorMask() does exactly that, so tColor can be removed almost certainly.
Presto: Based on feedback I've had with GapiDraw, people in most cases want to know where on the edge of the sprite the collision occured (to show visual effects in the correct place). That's why I never added midpoint subdivision tests to GapiDraw pixel intersections...
drgoldie wrote:
well, that also depends on the size of the sprite. if it fits nicely into the cache the difference won't be that significant.
but generally you are right. when i found out that the swizzle/unswizzle routines of PocketFrog where the performance problems i rewrote them to use macroblocks (say 32x32 pixels sized) which gave a huge speedup...
DANIEL
Just wanted to point out that my function doesn't require any additional memory allocation. You don't even need a destination surface to put them on. You can use the function to test for theoretical situations like "If this surface at point1 wanted to move up, would it collide with the surface at p2?" if you want to. Which means you could abort the move.
So if memory usage is a concern, this might be a feasible alternative to allocating additional space. The only reason I can see for allocating that space is to avoid having to use the GetPixel() function. I'm sure creating a 2-D array of booleans to store the transparency data would be much faster and wouldn't be that big, but that all depends on the number and size of the surfaces.
-John
Using GetPixel() might not be terrible if the compiler inlines it. If not, I thnink it probably should be manually inlined. That was one of the problems with my VGAText function... Looping through the data calling SetPixel() continually is not an optimal solution, unless it gets inlined (and I never checked to see if it was).
Johan wrote:
On some devices, changing
for(int x = x5; x <= x6; x++)
for(int y = y5; y <= y6; y++)
into
for(int y = y5; y <= y6; y++)
for(int x = x5; x <= x6; x++)
would result in at least 3x performance increase.. :)
That won't be true with the next PF release. I would recommand using GetPixel() for now is this will provide (close to) optimal performances with PF/PocketHAL.
Presto wrote:
So if memory usage is a concern, this might be a feasible alternative to allocating additional space. The only reason I can see for allocating that space is to avoid having to use the GetPixel() function. I'm sure creating a 2-D array of booleans to store the transparency data would be much faster and wouldn't be that big, but that all depends on the number and size of the surfaces.
I suggest creating a "Sprite" class that wraps both the image Surface and a collision mask (or any other collision related data).
The Sprite class could even support multiple animation frames, each with it's own collision data.
Thierry wrote:
I suggest creating a "Sprite" class that wraps both the image Surface and a collision mask (or any other collision related data).
The Sprite class could even support multiple animation frames, each with it's own collision data.
But don't make it required in any way. There are people like me who do not use sprites for their game objects.
mlepage wrote:
But don't make it required in any way. There are people like me who do not use sprites for their game objects.
The thing is collision detection has nothing to do with a graphic Surface. The fact that some people want to do it this way is just one of the many possible implementations.
I would personally never use the graphic image to conduct collision detection. It is inefficient and not really flexible. What if you want to have different collision zones on your sprite? What if some non-transparent pixels should not be considered "solid"? What if you use rotated images for your sprites? There is also faster ways then comparing pixels!
On the other hand everybody needs collision detection and I don't see an easy way to make everyone happy.
These are the reasons collision detection isn't there in PF. I am more then willing to add such a feature, but I want to make sure it satisfy most of the needs.
I know some people (including me) would like a sprite class that manage the animation of multiple images. It just seem a natural place to include other features such as collision detection, position and orientation.
I don't understand why you would say it has nothing to do with surfaces... Since everything in PF is a surface (excluding primitives of course), it seems perfectly natural to want to determine when two surfaces collide.
I think it's fair to say that most people have either implemented their own sprite class, or effectively treat surfaces other than the backbuffer as sprites (this is in fact what I do). In that case, I think it makes perfect sense to hang collision detection off the surfaces.
I think a sprite engine, even a very basic one, rolled into PF would be a big plus, even an advantage over GD (which is what this thread was about in the first place

). Even if it did nothing more than collision detection and animation frame management, I think it would be a big plus.
But in the absence of that, a simple collision detection of surfaces is sufficient, espeically if you don't roll your own sprite engine.
When you use a surface as a sprite, you do have to track it's position, don't you? This position isn't in the surface itself. To proove the point, the proposed function takes 2 surfaces and 2 positions as parameters!
So what I am saying is that collision detection data / algorithms should be implemented at that level, where the needed information (both surface data and position) is available.
When I thing about a sprite, I think about an image with a position and possibily other things.
So even though you might not have a class "Sprite" in your code, you do have the concept. Perhaps you call it "Entity" or "Game Object", I don't know...
Ok, I see what your saying. The fact is that PF doesn't have a proper "level" on which to do collision detection, that's really your point, hence the idea of adding a Sprite class above Surface. I agree with that 100%.
However, if we assume for a moment that a whole sprite engine, regardless of complexity, isn't going to happen (for whatever reason), then it might br a square peg in a round hole from an architectural standpoint, but on the Surface still makes some sense.
Then again, without an actual sprite engine, maybe the better option is simply a separate function, as Presto's for example, to do it. Yes, you have to pass the parameters, which is a sure sign that your not thinking OOP any more, but it still works.
Exactly.
Let's take Presto's function: where does it fit right now? Well nowhere unfortunately, except as a free standing function. But this isn't bad it itself: it can be useful to a lot of people.
What I am thinking right now is that I could host a forum on my web site where such function could be posted and commented/improved by others. Just like we are doing here. The difference is that it would be kept in a "knowledge base" forum.
Kind of like what's on my site already?
My previous offer about hosting the site still stands by the way. I'd spruce it up a bit for you of course.
I think everyone would rather you focus on PF coding rather than web site coding. I know I would anyway.
fzammetti wrote:
Kind of like what's on my site already?
fzammetti wrote:
My previous offer about hosting the site still stands by the way. I'd spruce it up a bit for you of course.
I think everyone would rather you focus on PF coding rather than web site coding. I know I would anyway.
I hate web site "coding"... So that works for me =)
Well, that's how I earn my living these days, so it's not a problem. Just don't expect it overnight. I'm neck-deep in an absolutely back-breaking project at work, not to mention my current game project at home, so it'll take a bit of time. I'll squeeze it in for ya though.
Actually, that probably didn't sound too good...

There are really several concepts at work here.
The first is the physical rigid body, with position, velocity, acceleration, mass, center of mass, etc. Your collision response will be based on these physical properties. For example, if a force is applied in a direction, the object will spin a certain way depending on its center of mass, not on its shape.
The second is the model. This is the physical shape of the object. The physical properties such as mass and position have no bearing on this. This model is used for collision detection.
The third is the graphics and animation. Again, this is another concept entirely. For example, I can run my game with the physics and models, but without the graphics. And I don't mean they merely aren't drawn; they aren't even loaded.
When you use the graphics image for the model (e.g. pixel by pixel collision detection) you are merging those two concepts into one. When you put all this and the physical properties into a sprite class, you are putting all the concepts into one. I prefer to keep them separate.
Let's look at Quake as an example. The entity is the physical object, and governs collision response. The model is the shape, but only for rendering, not for collision detection. For collision detection, it actually only uses the bounding box around the model and entity. For graphics, the 3D model specifies textures for each face. Some, e.g. in the world, can animate.
I think that's getting into areas that weren't originally intended, certainly not by me when I started the thread.
I think when you start talking about the physical characteristics of a game entity and how it reacts when it collides with other entities, your talking about a whole other concept separate from strictly a method for determining when an entity has collided with another.
The reason I started the thread and made the suggestion of adding some form of pixel-perfect collision detection to PF is because bounding rectangles in and of themselves is not sufficient in many cases. Think of nothing more than two surfaces with images that look something like these in them:
- Code:
------ |\ /|
\ / | \/ |
\/ ------
It should be obvious that a simple bounding rectangle check will not suffice in the case of the first surface coming downward towards the second, if centered on the second. The tip of the inverted triangle will go down into the surface some number of pixels before a real collision occurs. Thus, you need a pixel-perfect method at this point.
Now, how the objects react when that collision occurs is a whole other story, and something I definitely don't think should be included in a general-purpose sprite engine. You could do it of course, but you'd all of a sudden have all sorts of physical properties attached to each entity that have to interact in numerous and potentially very complex ways. That's something that should be left to the game developer, not the toolkit developer. That's my feeling anyway.
Here a pixel perfect collision sample for PF!
http://www.omnytex.com/pf/fileexchange/ ... _Test2.zip
I get about 45 fps on my Axim 300MHz, so it's quite fast... and it's Pixel Perfect.
Controls:
A - stops the asteroid from moving
B - starts the asteroid moving
C - stops the ship from moving
D-Pad - accellerates the ship
Tapping the screen exits the app.
-John
[edit] Changed the link and added the C-button [/edit]
fzammetti wrote:
It should be obvious that a simple bounding rectangle check will not suffice in the case of the first surface coming downward towards the second, if centered on the second. The tip of the inverted triangle will go down into the surface some number of pixels before a real collision occurs. Thus, you need a pixel-perfect method at this point.
You are partially correct.
Quake only uses bounding boxes, which isn't ideal but is adequate for Quake's purposes. All players roughly occupy a rectangular prism.
But pixel-perfect isn't the be all and end all. Collision detection still need not be based on the images. If you have a model, just do polygon to polygon collision detection. If your math is good enough, that can be better than pixel-perfect. You're doing collision detection at the sub-pixel level.
Anyways, it's true that not all games work this way. But some do. So it's best to ensure any basic sprite facilities have no overhead for those of us who are doing more advanced physics and movement.
Good points certainly.
The only thing I would say is that as a short-term solution, just to avoid having to code a whole sprite engine (in whatever form it might ultimately take), adding a pixel-perfect collision function would mean that PF developers can use that, or bounding rectangles, which is already supported, and that will probably suffice for 95% of the usage scenarios.
I hate to say it now, but for me this has become a moot point because I've decided to finish my current project with GapiDraw (dropping back to v2.03 most likely). When I started the thread, there was a much more immediate need, but that's not the case for me any longer.
However, I'm not at all sure what I'll use for the next project, and I'd like to see PF develop to the point where it's a no-brainer. It's pretty darn close now, this discussion point is just one more nudge in that direction for me.
the more i think about pixel-perfect collision detection the more i agree to fzammetti that for doing it with high performance it will be a high level function (e.g. using 1-bit collision masks to check 32bits at once, which requires additional memory and data structures).
but this is something which will not fit for many needs. maybe it would be better to strictly split pocketfrog into a low lever part (directly above pockethal) and a high level utility collection that would also have sprite support.
i think a quick brain storming for really needed features would be a good start. so what's missing in pocketfrog that really almost everybody could and would use?
I still argue that a pixel-based collision detection routine should be placed in low level graphics code.. I understand from a design point-of-view why it should not, but in some cases I believe it's in place to sacrifice design for performance. Here's why in GD:
(1) You need raw access to the surface buffer if you want to use 32-bit reads and writes. Swizzle/unswizzle won't do it since it's too much overhead. Even if the surface buffer is pitch aligned (i.e. xPitch is always 2 bytes using PocketHAL) you have to deal with odd memory alignment. You cannot use GetPixel even if it is being inlined.
(2) The routine has to be aware of the current display alignment (not an issue with Pocket HAL due soon), but also the current display rotation (also not an issue with Pocket HAL due soon as I understand it).
(3) As I understand it, PocketFrog only supports 555 or the 565 pixel format. No big deal if Pocket PCs is all you want to target. But GapiDraw supports 444, 555, and 565 (444 is vital since all Symbian devices uses it). In GapiDraw this is solved using template metacode: We get a unique intersection loop for every pixel format. Writing it yourself you have to write separate loops for every pixel format (unless of course you use GetPixel and color transform each pixel which is really slow)... It is my belief that the end-user should not have to deal with 444/555/565 pixel formats, which is unavoidable if you want to write an efficient pixel-based intersection routine.
Sure, pixel-by-pixel can be quite slow if not done correctly. But if you do it only on overlapping areas and do it in an optimized way it's actually quite fast (you can actually use two surfaces for each sprite - one with the graphics, and one with as little mask as possible for the pixel-based collision test)... And of course you can use several surfaces for each sprite, where each surface indicates a unique collision point.

Um. Ok, given this a bit thought then I guess that the suggestion with 1-bit masks that are always aligned in the same format is the most effective bit. And such a solution does not belong in the graphics library.
The reason I wrapped it so quickly in GD is that I just could take the BltFast routines and instead of copying pixels I just changed them to check pixels..
To much theory going on. I like Presto's approach, write the code. Thanks Presto I will have a look when I get a moment. I don't need to expand my sprite class at the moment as I only use it to animate simple 2D objects as most of the Space Treker graphics are 3D but after ST3 and a follow up game I'm writing a 2D isometric game so I need collision then.
ps: STIII is at about 70% of coding excluding sound. Graphics are about 95% done

You're welcome, Conan.
Note that I updated the .zip file, so be sure to get the latest version.
Also, since I was finally able to test it out last night, I found an error with the example source posted yesterday. The GetPixel() function belongs to the Rasterizer class, not the Surface class, so I had to add:
Rasterizer *r1 = pDisplay->CreateRasterizer(s1);
Rasterizer *r2 = pDisplay->CreateRasterizer(s2);
to the Collision function, and then use r1->GetPixel(...) and r2->GetPixel(...)
Just an FYI for those interested.
So, getting back to the main topic of this thread... What else does PF need to catch up with GD?
-John
Johan wrote:
I still argue that a pixel-based collision detection routine should be placed in low level graphics code.. I understand from a design point-of-view why it should not, but in some cases I believe it's in place to sacrifice design for performance. Here's why in GD:
It simple doesn't belong in a graphic library. Does DirectDraw provide collision detection? Does SDL? Does any other graphic library out there?
I don't see why it can't be implemented as efficiently outside. I would even argue that it can be implemented more effectively outside, where more is know about the game environment.
Johan wrote:
(2) The routine has to be aware of the current display alignment (not an issue with Pocket HAL due soon),
This only goes to proove my point: collision detection should not have to deal with graphic issues such as display alignement. You ended up implementing a much more complex piece of code because of that. Doing it a higher level means simpler code.
Johan wrote:
(3) As I understand it, PocketFrog only supports 555 or the 565 pixel format. No big deal if Pocket PCs is all you want to target. But GapiDraw supports 444, 555, and 565 (444 is vital since all Symbian devices uses it).
All pixel formats you named used a 16 bits ineteger to store them. Hence you can use the exact same code.
Presto I am trying your sample. I am downloading PocketFrog now.
Thierry, is 0.6 the latest version? I thought I heard 0.7 mentioned somewhere. Please put a release date on the web page in addition to the version number. Also, you should mention that it is the official PocketFrog site so I know I'm not on a mirror or other download site (which might be out of date).
Thierry, are you in Canada, whereabouts? (Private message if you wish.) I'm in Kingston, ON.
OK how do I build the collision test? I dropped it in the sample directory of PocketFrog 0.6.0, but neither the PC nor PPC projects will build for me. The former can't find PocketFrog.h, the latter can't find afxres.h.
C'mon, people, use *relative* paths. Or did I just put it in the wrong directory?
I managed to get the PC version built with 0.7, but not the PPC version.
I think the asteroid should stay in one place so the FPS is easier to judge. Maybe have it stay in one place for a few seconds, then another, maybe about 4-5 nice places. Then we can report FPS for those 5 positions.
I altered the code but it's hard for me to tell if it is an improvement or not.
You can stop the asteroid by pressing button A (1 on the keyboard). Check out the earlier post for all of the controls.
OK I managed to get the PPC build working by re-extracting it into the samples directory, adding /I"..\.." as a compile option (to find PocketFrog.h), and copying the ARM lib into the directory (too lazy to find the equivalent of /L option).
This makes it run as a sample without altering any global directories (which I do not like to do).
I will make sure that the path issues are easier to work with in the next release.
Both PocketFrog and PocketHAL will have their includes and their libraries in the same place. This way you will only need to add one include directory and one library directory in your environment.
I think we still need to lock down what the intersection function is doing. Is it just returning whether there is an intersection or not, or is it trying to find out where the closest intersecting pixel is? Maybe we need both?
I think we need to lock down the test app. It has to be easy to repeat tests with consistent measurement, and we have to decide which kinds of images are in our test suite. This may have an impact on the effectiveness of various strategies.
I tried two approaches. The first is to do the check 4 times, skipping even/odd pixels in both directions, in an attempt to discover a collision faster.
The second is to start at the middle row, then work outwards, in an attempt to discover a collision faster.
I can't tell if those approaches are faster or not, with the test app. I think we need to lock down a position of the sprites for testing (or various positions). Also, maybe we should use the media timer and measure the actual time of the call to the collision function (or 100 calls).
Hi everybody.
As you can see I don't have a lot of posts(!) because I use GD (but considering moving to something else due to 2.05 licensing terms

)
Your articles deals with sprite collision.
I do agree with Thierry (BTW: are you french?) that sprite collision should not be too low level. It could be used for lots of things not all directly linked to graphics :
- Compute dirty area
- Compute new speed vector for an object due to collision
- "Normal sprite collision" (death of heroe - You!)
I just submit this samll articles that was written by ... Micro$oft !
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfgaming.asp
I have found this one interesting because for any pixel to pixel compares it is precomputing the "interesting" rectangles to check for.
I think it's also interesting for newbies to read this one because it explains a lot of tips and tricks.
I hope that it might be helpfull for anybody and that once in a next release we could find sprite-like class.
Bye
jcredk