PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Collision Detection

Having some problems with the WM_COLLIDE handler. It detects the collision, and stops the sprite, however it seems like it's still trying to move - ie I have to hold down the opposite direction to bring the speed back to 0 before he'll move left again.

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

WM_KEYDOWN:
KeysPressed$[wParam$] = 1;

WM_KEYUP:
KeysPressed$[wParam$] = 0;

WM_COLLIDE:
SetSpriteSpeedX(wParam$, 0);
PostMessage(hWnd$, WM_CLOSE, 0, 0);
end;
return(ok$);
end;


and what I'm using for the movement:

Code:

if (KeysPressed$[keys.vkLeft$])
SetSpriteSpeedX(player$, SpriteSpeedX(player$) - .001);
end;
if (KeysPressed$[keys.vkRight$])
SetSpriteSpeedX(player$, SpriteSpeedX(player$) + .001);
end;


Likely a stupid question, but I can't figure out what's causing it. If I set the sprite speed to 0, shouldn't it start subtracting from 0?

Also a small note - I noticed the second line doesn't get executed regardless of what I put there, is that normal or is that another stupid error?

[edit] Heh, while I'm at it, DrawSprite asks for the spriteid, is that what I set the variable to when loading the sprite, ie

worldtest$ = LoadSprite("c:\\ppl\\plat\\image.bmp", -1, 1, 0, NULL);

would worldtest$ be the spriteid? that doesn't seem to work here.

[edit2] Doh, now if I put a sprite under the player, and set the gravity, it falls OK, but I'm unable to move due to the fact it's constantly finding a collision. Is there anyway to detect collisions on the separate axis?
The SpeedX and SpeedY are always added to the sprites X and Y position every timer cycle.

The SpriteID is the returned value from LoadSprite and CloneSprite yes.

Can you send me your code on msn? I will take a look and fix whatever has to be fixed and give you some advices at the same time.
Has anyone successfully gotten a sprite to move on another sprite? It's looking more and more like a limitation of the physics engine at the moment. That doesn't quite make sense - if it collides, the speed should be at 0, instead of keep increasing right? And when there's nothing to collide with, the speed should start accelerating again.

kornalius: I added you to MSN, however I've never seen you on yet.
Yeah, I just got it working on my PC. You should be able to contact me now. I can see you!
If the sprites collide, PPL moves the colliding sprite to it's old position so it doesn't touch the other sprite. But the speed doesn't change by default. I will set the SpeedX and SpeedY to 0 when there is no elasticity.
Alright excellent, however could it be possible to detect which direction/axis the collision is occuring?

ie you fall when you jump into a sprite, however you can stick to the sides, and top without being able to move horizontally.

This might cause problems, because if you're on a flat surface, and speedy is constantly being set to 0 since youre always technically colliding, you might not be able to actually go up ever? In that case, it could detect the direction, and not allow positive x movement, negative x movement, and so forth.

Maybe having a WM_COLLIDEX and WM_COLLIDEY opened up to the user would be great too. Or maybe I'm thinking into this too much since it seems like I'm barely writing any code!

--

Also, I'm slowly preparing to start the actual tile-based part, but this code does not seem to place the sprite in the right position - it's just in the top left, and with no collisions:

Code:
proc InitWorld

t1$ = loadsprite("c:\\ppl\\plat\\image.bmp", G_RGB(255, 0, 255), 1, 0, NULL);

SetSpriteCollide(t1$, "MARIO");
DrawSprite(t1$, 150, 100);
SetSpriteID(t1$, "WORLD");

SetGravity(0.00001);

end;


I'd like to be able to just load all the sprites in one part, then parse through the level matrix (sounds fun) drawing nothing when it sees a 0, drawing t1% when it sees a 1, and so on, unless I've got the wrong idea about drawsprite?
You mean getting the angle (or previous speedx and speedy) of the collision?

Yeah that is feasible, I will come up with something.

Btw, I have fixed the WM_TIMER event to be independant from the FPS. So a timer set to 5 ms will be be triggered only every 5ms, no matter what speed you are running. It's quite basic but it should do. Let me know if you have a better idea.
Oops, edited my post afterward, revised some of the wording.
First thing first, Sprites are automatically drawn in the WM_PAINT event handler if you setup one, RenderSprites does it for you. All you need to do at load time is MoveSprite(Sprite$, X, Y);

I still don't understand your question about the WM_COLLIDEX... Maybe I am just too stupid, sorry! :(
So how would I redraw the same sprite several times with minimal loss on memory performance?

As for WM_COLLIDEX, I was thinking of an event handler when it detects a collision on the X axis, and a handler on the Y axis, or if you go with detecting per driection rather than axis, an evfent handler for north collisions, east collisions, etc.
The WM_PAINT event is called every frame. So no need to redraw the sprites manually, they are already redrawn every frame with the RenderSprites() function.

I will do some brainstorming tonight, because I can probably give the WM_COLLIDE another parameter with the angle or the x,y collision. I will have to review my code first.