Author Topic: Multiple masks?  (Read 7098 times)

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Multiple masks?
« on: 2008-01-25, 09:08:57 AM »
Hi guys!

I encountered another problem in my project, and I wondered if you had any suggestions. 

My project is a fighting game and now I am at the point of testing collisions between the fighters.  The thing is, I wondered how it would be possible to set 2 collisions masks on a sprite.  Or one collision masks that has 2 "areas".  Because, for example, if a fighter kicks the other fighter, I want to be able to detect if the collision was made by the leg (and then it damages the second fighter) or any other body part (that shouldn't cause any damage).

I tried to find a way, but I could'nt.  So I decided to cut out the leg of the fighter and create another sprite with the leg.  So when a fighter kicks, I place the missing part of the leg, and then I can calculate what hit the second sprite.  But it is a lot of work to synchronise both animations (the leg with the rest of the body).  And it seems to glitch sometimes, and I suppose it would be a lot of efforts to debug it.  So I wondered if there was a better way to do this.

Do you have any suggestions?

Thanks a lot! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Multiple masks?
« Reply #1 on: 2008-01-25, 12:18:59 PM »
There's no built-in way, and I can't think of a really simple way to do exactly what you want right now.  It may be a good idea for the future, but for now, I have a simpler idea: Does it really matter if it's the leg?  What if you do some simpler detection?  If the kicking player is to the left of the kicked player, and the kicking player is facing right and in a "leg is high" frame, and the fighters are sufficiently far apart such that no other part could really hit them anyway, then a kick is recorded.  And vice versa for the other way around.  You already know the approximate sizes of the players, so this should work.
Edward Dassmesser

Richard Kain

  • Regular
  • **
  • Posts: 23
    • View Profile
Re: Multiple masks?
« Reply #2 on: 2008-01-25, 01:48:10 PM »
I must be missing something. Aren't sprite definitions made up of framesets? And aren't framesets in turn made up of tilemaps? And isn't it true that each element of a tilemap is able to have its own alpha values? As such, shouldn't it be possible for every frame of a sprite's animation to have its own, unique alpha mask? And can't collision be tested based on the sprite's alpha value?

So if you are using exact collision detection based on the sprite's alpha values, don't you just need to make sure that each frame has its own unique alpha value? Or am I just crazy with this assumption? I've been importing a crap-ton of sprites into the trial project that I'm playing around with, and every single frame has its own unique alpha values.

Also, I've been thinking that double-checking for collision detection would probably be the most efficient method for exact collision testing. For the more experienced users here, tell me if I'm wrong. If you were to use an "If" statement to test for basic rectangular hit detection, you would get the most efficient collision detection in terms of speed, yes? And if you put a call to alpha collision detection within that "If" statement, that collision check would only be performed if the rectangular check had evaluated to true, correct? Ideally, using this method for any instances of exact alpha collision detection would result in better performance, since the alpha collision check would only be performed if the faster rectangular checking had already proven true. Just tell me if I'm off base there, because I'm going to be getting more in-depth on collision testing in my trial project soon.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Multiple masks?
« Reply #3 on: 2008-01-25, 02:55:03 PM »
Well, finally it continued working with 2 sprites rather than one sprite and detection by coordinates like you suggested durnurd (that was a good idea though.  I think I will try this next time.)

To Richard Kain:
Well, the thing is that I want to place a 2 sections mask on the same frame.  All my frames have their own alpha mask, but on a particular frame I want two masks.

It was a lot of work to synchronise the animations of the sprites (I found out that the "glitch" originated form my code, so it is solved), but it works well now.

Thanks a lot! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Multiple masks?
« Reply #4 on: 2008-01-25, 03:02:54 PM »
maybe you could try this:
the frame where the leg is kicking. make somehow in this frame the body a little bit less opaque then the leg. set the alpha mask so, that in this frame only the leg is seen. when there is collision, you can be sure it is from a kick. like bluemonk said a few times to me: "noone will notice when your picture has an alpha of 254 than 255."

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Multiple masks?
« Reply #5 on: 2008-01-25, 04:03:30 PM »
To Morgengrauen:
Yes, it would be a good solution, but it doesn't solve another problem: if the body must also detect a collision if it is attacked by the other fighter.  Thanks for the suggestion
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

MadMath

  • Regular
  • **
  • Posts: 20
    • View Profile
    • eMidrash.net
Re: Multiple masks?
« Reply #6 on: 2008-01-25, 04:18:17 PM »
Could you make two states (one for each collision mask) then have it change to the state it needs just before each collision detection rule. For example, you could add a set of rules to change to state 1 then check for collision (to check if the fighter has been hit) then change to state 2 and check for collision (to check if the fighter hit with its kick). I haven't tried it myself, so I don't know if it will work right.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Multiple masks?
« Reply #7 on: 2008-01-25, 10:28:13 PM »
Also, I've been thinking that double-checking for collision detection would probably be the most efficient method for exact collision testing. For the more experienced users here, tell me if I'm wrong. If you were to use an "If" statement to test for basic rectangular hit detection, you would get the most efficient collision detection in terms of speed, yes? And if you put a call to alpha collision detection within that "If" statement, that collision check would only be performed if the rectangular check had evaluated to true, correct? Ideally, using this method for any instances of exact alpha collision detection would result in better performance, since the alpha collision check would only be performed if the faster rectangular checking had already proven true. Just tell me if I'm off base there, because I'm going to be getting more in-depth on collision testing in my trial project soon.

I believe this is done automatically in the TestCollisionMask function. Looking... yes.  The function makes sure that the two sprites in question overlap in their bounding box before trying anything more specific.
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: Multiple masks?
« Reply #8 on: 2008-01-26, 08:02:47 PM »
Also, I've been thinking that double-checking for collision detection would probably be the most efficient method for exact collision testing.

Checking collision mask intersection only occurs on the overlapping rectangular area of the sprites, so the extra test isn't necessary to optimize your collision detection.  You'll notice, if you look at the "TestCollisionWith" function in CollisionMask.cs in the SourceCode folder of your project, one of the first things it does is check that the masks have some overlap, and if they don't it returns false immediately.

I see durnurd answered that before I did, but you still might want to take a look at CollisionMask.cs if you're interested in how optimized it is.