PDA

View Full Version : Sprite Collision Detection


Paul Fielder
10-19-2006, 04:56 PM
?! I have been playing with a sprite moving over a rectangle drawn with g_FillRect but can't seem to detect when it moves over / Collision occurs or can this only be don with sprites and not the background?

kornalius
10-19-2006, 05:19 PM
Collision detection is only on sprites.

However you can possibly fill the collision detection grid manually in the rectangle but it's a little complicated, I can show you how if you want. I suggest you only use sprites, its faster and easier.

Paul Fielder
10-19-2006, 05:32 PM
kornalius,

Guessed as much, I had a play with detection grid, couldn't get it to work at all so I have started playing with particals.

I'm enjoying the playing but feel that some of the manual needs more examples as I speend almost an hour tapping on My AXIM only to discover I'v been barking up the wrong tree !sad

kornalius
10-19-2006, 06:17 PM
Sorry to hear that Paul but it's part of the learning process you know. You should have seen the amount of time I spent learning how to program the PocketPC when I started writing PPL.

The GameAPI section in the help is still far from being done. We are working on it don't worry.

Here is how it is done:

After having created all your sprites do the following:

GameCollide(True); // WM_COLLIDE in GameProc will be called.

SetGridCells("collideid", 50, 50, 100, 100); // Sprites with collide id of "collideid" will collide at rectangle 50, 50, 100, 100.

Make sure "collideid" is a lowercase string. This bug has just been fixed for 1.06.

Then in WM_COLLIDE for your GameProc, do the collision code.

There is another bug that I have fixed just now in 1.06. Tomorrow your code will be working fine.

Sorry about the bugs in these functions, I haven't had the time to test them properly after the last beta version 0.97.

Here is a small stupid test I worked with:

// Default Advanced Game API skeleton

#include "GameAPI.ppl"

func mainproc(hWnd$, Msg$, wParam$, lParam$)
ok$ = true;
case (Msg$)
WM_CLOSE:
ShutGameAPI(hWnd$);

WM_KEYDOWN:
PostMessage(hWnd$, WM_CLOSE, 0, 0);

end;
return (ok$);
end;


func GameProc(hWnd$, Msg$, wParam$, lParam$)
case (Msg$)
WM_PAINT:
// Clear the screen with black.
G_Clear(0);
// Render sprites on screen.
g_fillrect(60, 60, 90, 90, G_RGB(100, 100, 100));
RenderSprites;
//g_textout(null, "Press any key to exit", DVT_CENTER, 0, 20, G_RGB(255, 255, 0));

WM_COLLIDE: // Collision between sprites in wParam$ and lParam$.
G_showmessage("hi");

end;
return (true);
end;

func SpriteProc(Sprite$, Msg$, wParam$, lParam$)
case (Msg$)
WM_TIMER:

end;
return (true);
end;

func WinMain
h$ = newform({GAPI}, {GAPIClass}, &mainproc);
ShowWindow(h$, SW_SHOW);

ShowFPS(true, G_RGB(255, 255, 255));
InitGameAPIEx(h$, &GameProc, 240, 320, true, 5, 0);

Global(MySprite$, i$, j$);
struct(MySprite$, TSprite);

i$ = loadsprite(AppPath$ + "image.bmp", G_RGB(255, 0, 255), 1, 0, NULL);
SetSpriteProc(i$, &spriteproc);
SetSpriteCollide(i$, "PLANE");
SetSpriteId(i$, "PLANE");
AddSpriteOption(i$, SO_CHECKCOLLIDE);
setspritedirection(i$, 45, 1);

// Make sure WM_COLLIDE event is called in the GameProc
GameCollide(true);

SetGridCells("plane", 60, 60, 90, 90);

return (true);
end;

Paul Fielder
10-19-2006, 06:40 PM
Kornalius,

I agree with your comments about “ all about learning a language”, however, non-the-less frustrating when developing on the Pocket PC platform.

I’m really enjoying developing and seeing the small steps building into a good grounding with PPL; your support and guidance was the main incentive to purchase the Pro version safe in the knowledge this if I hit a wall there is always someone to ask.

kornalius
10-19-2006, 07:01 PM
You are right Paul developing on the PocketPC is not an easy task. :)

I am happy to see that our efforts are appreciated. Thanks.