Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / GetSpriteHit( ) for a range of Objects

Author
Message
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 20th Jan 2013 20:13
Hi all,

in gdk we have a function that checks for mouse hits
for a range of objects, from n to n+a for example.

Would it be possible to get the same for agk ?

instead of:

agk::GetSpriteHit( agk::GetPointerX(), agk::GetPointerY() )

it would be:

agk::GetSpriteHit( agk::GetPointerX(), agk::GetPointerY(), SpriteStartNumber, SpriteEndNumber )



cheers
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Jan 2013 21:39
It's easy enough to create your own function to do that.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 21st Jan 2013 23:26
How do I prevent the function
agk::GetSpriteHit( agk::GetPointerX(), agk::GetPointerY() )
from searching thru all the Sprites ?

Answer: Use SetSpriteGroup( SpriteID, group )
and GetSpriteHitGroup( group, x, y ). Right ???

If I use
agk::GetSpriteHitCategory( categories, x, y )
what are the 16 categories, where do I find info on it ?


"It does not take into account the transparency..."
means that even if the Sprite is invisible, it will still
return its ID. Right ???


Sorry for being so slow,
but I think I slowly get it!

cheers
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jan 2013 16:25
For categories, it is a bit mask. Each of the 16 bits is it's own category.

So, 1 is category 1, 2 is 2, 3 is 1 and 2, 5 is 4 and 1, 7 is 1 and 2 and 3, etc.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jan 2013 16:26
And, yes, even if a sprite is invisible it will count as a contact. You have to turn physics off on a sprite for it not to be included.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 22nd Jan 2013 23:57
Where do I get Information what the
categories are ?

cheers
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 23rd Jan 2013 00:12 Edited at: 23rd Jan 2013 00:14
You set them as required:

SetSpriteCategoryBit

SetSpriteCategoryBits


And then check against them

GetSpriteHitCategory
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 23rd Jan 2013 02:06
My problem is , that I don't know what

1 stands for, what 1 does ?

I don't know what 4 stands for,

what 4 does ?

There must be a place somewhere where I can

see what the 16 categories mean.


Thanks
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 23rd Jan 2013 02:25
Or does it mean that I define the categories and what they do ?


If Yes, what could be possible categories ?


cheers
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 23rd Jan 2013 09:35
Yes you define the categories and what happens.

They can be anything you want.

For example you could have:

Red Team
Blue Team
Yellow Team

assigned to the first three category bits (you do not assign what they are in code you just need to note them for your own reference)

So any sprite with the first category bit set will be a member of the Red Team.
AgentSam
12
Years of Service
User Offline
Joined: 14th Mar 2012
Location: Virtual Space
Posted: 23rd Jan 2013 10:12
joachim,

while it isn't exacly clear what your intended use will be, which might provide more information on the best solution for your usage case... it still seems to me that you fixated on the SetSpriteCategoryBit commands, and forgot about SetSpriteGroup, although your quote in the second post contains them.

My suggestion would be to use SetSpriteGroup instead of SetSpriteCategoryBit ! Really. It is much more effective for detecting hits on a range of sprites. And it isn't limited to only 16 categories like the category bit functions. And - they can also be used together for very fine grouping and categorization.

For "SetSpriteGroup( iSpriteIndex, group )", the group parameter is a 32-bit signed int, which will allow you to create as many as 2,147,483,647 groups (assuming only positive values are allowed, the docs aren't very specific about this.)

And within groups, you could - if you needed to - assign categories, so that you can have eg. RedBalls, BlueBalls, GreenBalls - all of which belong to the "Balls" category bit (which you select yourself).

But let's keep it simple. Forget about the category bits for now. Just assign your range of sprites to the same group using SetSpriteGroup, and then check if a hit occurred on any of them, using GetSpriteHitGroup.

Cheers,
AgentSam
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Jan 2013 12:09
I agree with AgentSam here. Using groups is far easier to get your head around. I use it all the time for so many different uses!


this.mess = abs(sin(times#))
tornado
19
Years of Service
User Offline
Joined: 21st Jul 2005
Location:
Posted: 23rd Jan 2013 14:51
What's the use of categories then? if we have 2,147,483,647 space for groups, what's the purpose of this 16x category multiplier? I don't believe categories are useless, I believe I can't get their meaning from docs

TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 23rd Jan 2013 14:57
A sprite can only be a member of a single group so you could for exampe use the category bits to define sub groups or different states that are common across multiple groups.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Jan 2013 15:42 Edited at: 23rd Jan 2013 15:44
The category usage allows for exactly 16 categories and a sprite may be a member of any of them.

The categories themselves have no meaning, it is all in the way you use them.

For instance, you could assign all walls as categories 1 and 2, assign ghosts category 3, heroes category 1 and bad guys category 2.

Then you could use GetSpriteHitCategory with the value b0000000000000011 (3) to see if a hero or bad guy hit a wall. This would ignore hits between ghosts and walls because the ghosts are not the correct category.

The confusion comes with the commands because the SetSpriteCategoryBit uses the bit position as an input and the the SetSpriteCategoryBits and GetSpriteHitCategory use bit mask fields and they use hex values to display the examples in the documentation.

This table shows the hex and bit masks for the 16 categories:
hex mask - bit mask - category
0x0001 - b0000000000000001 - 1
0x0020 - b0000000000000010 - 2
0x0040 - b0000000000000100 - 3
0x0080 - b0000000000001000 - 4
0x0010 - b0000000000010000 - 5
0x0020 - b0000000000100000 - 6
0x0040 - b0000000001000000 - 7
0x0080 - b0000000010000000 - 8
0x0100 - b0000000100000000 - 9
0x0200 - b0000001000000000 - 10
0x0400 - b0000010000000000 - 11
0x0800 - b0000100000000000 - 12
0x1000 - b0001000000000000 - 13
0x2000 - b0010000000000000 - 14
0x4000 - b0100000000000000 - 15
0x8000 - b1000000000000000 - 16

To set or check multiple categories, you add the hex or bit mask values. To check for categories 1 and 2:
0x0001 + 0x0002 = 0x0003
b0000000000000001 = b0000000000000010 = b0000000000000011

In both cases above, if the values were being used as 'proper' numbers, the decimal value would be 3. But that is not how they are used in the Category commands. The commands use bit masks as shown above.

Maybe this helps clear it up a little bit.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 23rd Jan 2013 16:53
This corresponds nicely to the Pascal SET type. There are actually 17 cases, because the empty set [] is a valid result.

Using sets can eliminate a very large number of conditional statements and Pascal compilers will optimise the code.

It is normally better to use logical rather than arithmetic operators on bit masks. For example, it's less fraught with danger to use the OR operator (in whichever language) than use +

Pascal (right from the start) gives you a variety of powerful and controlled operations handling sets:

+ Union
- Difference
* Intersection
>< Symmetric difference
<= Contains
include include an element in the set
exclude exclude an element from the set
in check whether an element is in a set

The Intersection operator returns a subset including elements which are in both parameter sets:

I := A * B;

Careful use of things like this can save a lot of impenetrable code, and iteration.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
joachim
12
Years of Service
User Offline
Joined: 21st Feb 2012
Location: Vancouver Canada
Posted: 23rd Jan 2013 20:29
Thanks to all of you,

so the differences between "Groups" and "Categories"
are

A sprite can only be a member of a single group, but
mutiple categories.

A Sprite is asigned to a group by
SetSpriteGroup( SpriteID, GroupID)
and to a category via
bit mask.

Questions:
0x0001 + 0x0002 = 0x0003
b0000000000000001 = b0000000000000010 = b0000000000000011
How does this look in actual code ?



How does it behave in Runtime ?

Can I change group membership and category membership
in Runtime, are there examples anyware ?

Is this subject covered in the book ?


Thanks again.
regards


PS:
Why is there no documentation on a subject as important as this.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Jan 2013 22:21
Quote: "Questions:
0x0001 + 0x0002 = 0x0003
b0000000000000001 = b0000000000000010 = b0000000000000011
How does this look in actual code ?"

To assign multiple categories (1 and 2) to a sprite (note the 's' on the end of the command):
SetSpriteCategoryBits(spriteID,0x0003)
--or--
SetSpriteCategoryBits(spriteID,b0000000000000011)

Of course, you can drop all the leading zeros, they are just for the 'pure' 16 bit example.

To check for hits:
spriteID = GetSpriteHitCategory(0x0003,someX#,someY#)

Quote: "How does it behave in Runtime ?"

How do you mean?

If you set the category bits, you can then use SetSpriteCollideBits to set default collision behavior. If you have physics turned on and are using forces or impulses to move things (letting the physics engine handle the collisions and bouncing), if two sprites do not share a collision and category bit, they will not bounce or interact within the physics world.

The command GetSpriteHit(someX#,someY#) will return any sprite at that display point.

The command GetSpriteHitCategory will return any sprite with the right category at the indicated point.

The command GetSpriteHitGroup will return any id for a sprite with the correct group.

And check out the various RayCast commands under Physics for another way to use groups and categories.

Quote: "Can I change group membership and category membership
in Runtime, are there examples anyware ?"

Yes, to changing group/category in runtime. This makes for some interesting capabilities, like making something invisible to other things on a temporary basis.

And you can use both Group and Category on the same sprite, they don't 'know' about each other.

And "I don't know" to the rest of that question and all others.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-11-24 05:25:37
Your offset time is: 2024-11-24 05:25:37