Author Topic: Collision Masks  (Read 13118 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Collision Masks
« on: 2007-11-21, 03:34:38 PM »
it seems the bitmap masks for sprite collisions don't work how (i) expected. i changed the mask alpha level to a higher count, and so my player should have an perfect mask. but it shows no effect.
i wondered how the creator of the car sprite had made this rectangles. than i discovered this, but they stay at the upper left corner. how to shift them to cover my player sprite with them?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Collision Masks
« Reply #1 on: 2007-11-23, 10:38:49 AM »
The collision mask always corresponds to the location of the sprite, but the image of the sprite does not always have to correspond to the location of the sprite.  To shift the location of the collision mask relative to the image of the sprite, you have to use the frameset editor to move the frames that the sprite uses.  The intersection of the axes represent the position of the sprite, and you can adjust the graphics relative to this origin.

Edit: The preview you showed above is incorrect -- I just noticed there was a bug in the code that showed that black rectangle in the wrong place.  I've fixed it, and the fix will be included in beta release 2.  The black rectangle should have been shown directly on top of the car with just a couple pixels of the image showing around the edges. But my above statement is still correct (and the actual collision detection was working correctly).
« Last Edit: 2007-11-23, 11:11:16 AM by bluemonkmn »

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #2 on: 2007-11-27, 03:01:27 AM »
i don't understand why a value, greater than 1, not works for my mask-alpha level. look at the pic where the zeppelin is drawn completely black. the game doesn't use this mask. it uses the value for the rectangle.

and one more thing i don't understand. i shiftet the sprite image in the frameset editor as shown. note the y-shift is greater than the x-shift. i had problems to position the rectangle mask correctly, the more i tried, the more it failed. then i noted: in the window for adjusting the collision mask, the x-shift is greater than the y-shift. could there maybe be a mistake, a swap of the values?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Collision Masks
« Reply #3 on: 2007-11-27, 07:45:55 AM »
i don't understand why a value, greater than 1, not works for my mask-alpha level. look at the pic where the zeppelin is drawn completely black. the game doesn't use this mask. it uses the value for the rectangle.
The mask only applies when using TestCollisionMask with two sprites that have masks.

and one more thing i don't understand. i shiftet the sprite image in the frameset editor as shown. note the y-shift is greater than the x-shift. i had problems to position the rectangle mask correctly, the more i tried, the more it failed. then i noted: in the window for adjusting the collision mask, the x-shift is greater than the y-shift. could there maybe be a mistake, a swap of the values?
Yes, this is exactly the bug I mentioned above.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #4 on: 2007-11-27, 07:52:11 AM »
oh sorry, i didn't get that. cool, then i can wait for the fix.
i thought the collision mask would also be applied for collisions with solid tiles. i guess i have to make that rectangle stuff for collision with tiles, and than set the alpha mask to 1 or higher, for sprite collision. am i right?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Collision Masks
« Reply #5 on: 2007-11-28, 07:07:06 AM »
Yes, collision with tiles always uses the rectangle (as if alpha mask level were at 0).  You can preview how the sprite will react with tiles by looking at the preview when the mask level is at 0 (once the bug is fixed).  But the specified mask level actually only applies to sprite collisions.  Of course you can also use the rectangle for sprite collisions by setting the alpha mask level to 0 for both sprites (or use TestCollisionRect instead of TestCollisionMask).  Detecting rectangle collisions between sprites is significantly faster/simpler than using the mask.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #6 on: 2007-11-28, 07:20:34 AM »
i know it's simpler and faster to use the rectangle. but it is a very ugly solution if the sprite is often in diagonal state (given a cylindrical form of that sprite).
perhaps there is the possibility to rotate the mask rectangle, what do you think?

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #7 on: 2007-11-28, 08:51:53 AM »
now that i know how to shift the sprite in the frameset, there occur two another problems:
the zeppelin got a bag at its downside. because of the image shifting the air ship hops in some frames higher than in others. the bag isn't placed right then.
but i will draw the zeppelin and the bag in one image, than there is no worry about placing and that stuff. that is sure a lot easier.

the second problem:
the zeppelin sprite is drawn the way that the center is always in the center middle. so it is in the same place, even when it's sinking or rising. now i have done the shifting for correct placement of the mask. but the shifting is quite different from frame to frame. when i run the game and change states, the entire screen leaps here and there a few pixels. it is a very obvious and ugly effect.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #8 on: 2007-11-28, 10:01:20 AM »
another thing is, when you have shifted an image in the frame editor, close that and return later, the values for scale/rotate/transform are at zero again. that makes it very difficult further to manipulate.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Collision Masks
« Reply #9 on: 2007-11-28, 09:47:16 PM »
The values for changing in the frame editor are based on the current frame state so you can see how far you are changing it "locally" (based on where it was).  The actual values based on the "zero" state are shown when the frame is selected in the frameset editor on the bottom.
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Collision Masks
« Reply #10 on: 2007-11-28, 10:26:35 PM »
now that i know how to shift the sprite in the frameset, there occur two another problems:
the zeppelin got a bag at its downside. because of the image shifting the air ship hops in some frames higher than in others. the bag isn't placed right then.
but i will draw the zeppelin and the bag in one image, than there is no worry about placing and that stuff. that is sure a lot easier.

Instead of putting it in the same graphic sheet image, you could just put it in the same sprite frame by setting the repeat count of the first sub-frame to 0, and adding the correctly positioned bag frame after the zeppelin frame in the same sprite state.  Was that your plan?

the second problem:
the zeppelin sprite is drawn the way that the center is always in the center middle. so it is in the same place, even when it's sinking or rising. now i have done the shifting for correct placement of the mask. but the shifting is quite different from frame to frame. when i run the game and change states, the entire screen leaps here and there a few pixels. it is a very obvious and ugly effect.

It sounds like you either need to have smaller scroll margins so the sprite can move more without scrolling the screen, or you need to specify a better Alignment parameter for SwitchToState and make sure that you are positioning the sprite correctly instead of just adjusting the frames to make it look correct when it's actually in the wrong place.  Keep in mind that the solidity rectangle (mask rectangle) always starts from the sprite's real position (regardless of where the frame is), and that rectangle determines how the scrolling works.  I have a hard time understanding exactly what your situation is, but maybe this will help.  If nothing else, you could submit your project to http://sgdk2.enigmadream.com/support/ and I could take a look.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Collision Masks
« Reply #11 on: 2007-11-29, 06:41:40 AM »
i know it's simpler and faster to use the rectangle. but it is a very ugly solution if the sprite is often in diagonal state (given a cylindrical form of that sprite).
perhaps there is the possibility to rotate the mask rectangle, what do you think?

You're welcome to customize the collision detection -- all the code is completely customizable and you could edit ReactToSolid if you know how to make it work like you want.  But I'll warn you that ReactToSolid is probably by far the most complicated function in the system (I've been working on variations of it for almost 20 years, and it's always the most complicated function in the project), and I don't think I have the skill to make it any more complicated.  :)

Of course if you don't care about many of the features that ReactToSolid gives you, maybe it can be simplified and then adjusted.  ReactToSolid is designed to handle hills and sliding and a few other things that you might not care about.  If you just want the zeppelin to stop before hitting a solid instead of adjust the velocity to get as close as possible and slide along the edges of the solid, maybe you could come up with an alternative ReactToSolid with a different set of features that some would prefer over the default.

ReactToSolid is in SpriteBase.cs if you want to try.
« Last Edit: 2007-11-29, 06:48:00 AM by bluemonkmn »

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #12 on: 2007-11-30, 04:02:41 PM »
i would definitiley never want to challenge a good programmer, not even a bad one. i may take a look at the code, but nothing more. i like the rule constructing kit very much, thank you. when a programmer says, a thing is complicated, i tend to believe him.
and the sliding behaviour is good enough for me.

as for your tip with the scroll margins:
i decreased the scroll margin values. when the player is in the middle of the screen, all is alright.  but mostly the player travels at the edge of the scroll margin as he flies through the map. (understandable?)
but the collision rectangles are quite different in their size. when he now changes his state to an image, which has a wider collision rectangle than that before, he is "pushed" innerwards. that can hardly be seen, but one thinks the entire screen is jumping a few pixels here and there.

i think this can hardly be prevented, but with good planning for the sprite. no sudden width-changing or so.

"or you need to specify a better Alignment parameter for SwitchToState and make sure that you are positioning the sprite correctly"

do you mean i have to take care of always using the same alignment parameter, e.g. "center middle"? i did that already.
or could it be i have to use some times different alignments, because of shifted frames?

why have i to shift the position of frames? is it only to determine the right position for the collision rectangle?

Instead of putting it in the same graphic sheet image, you could just put it in the same sprite frame by setting the repeat count of the first sub-frame to 0, and adding the correctly positioned bag frame after the zeppelin frame in the same sprite state

i am not quite sure what the advantage would be. when i paint the bag direct onto the ship, i have a better control of the position. hm.... okay... for later image style changes maybe....

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Collision Masks
« Reply #13 on: 2007-11-30, 05:45:23 PM »
If the bag is separate from the ship, it can move freely on its own.  For example, it could mostly follow the balloon, but be affected by wind resistance or momentum so that it swings a little.
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Collision Masks
« Reply #14 on: 2007-11-30, 05:56:42 PM »
you are right, now that it is a single sprite it swings nicely. i thougt about to paint that and therefore to animate the normal flight of the ship, but that would be more work. it is also no problem to drop it.