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 / GetSpriteHitGroup()

Author
Message
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 6th May 2018 16:24
Having trouble with this command. It doesn't recognize ANY hits. All seems to be in order otherwise; i.e. a valid group and set of coordinates are passed, and there is definitely a sprite to detect. What could possibily be the cause of the problem?
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 6th May 2018 16:36
Hi,

It's impossible to tell you what you are doing wrong if you don't show us what you are doing.
If you provide some code we can diagnose the issue.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 6th May 2018 16:38
are you using SetSpriteGroup(id,numofgroup)
because
GetSpriteHitGroup( group, x, y )
returns the spriteId within the group set with the SetSpriteGroup command
fubar
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 6th May 2018 16:44
The code looks like this, inside of a FOR loop:



Its really not feasible to show more than this; the code is just too extensive and complex to post. Nevertheless I know for sure that x and y are valid, as are the groups. All relevant sprites are assigned to one of those groups. it is possible for more than one sprite in or out of those groups to occupy the same space.

g1 and g2 always return zero no matter the situation.
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 6th May 2018 16:45
FUBAR: Yes I am using SetSpriteGroup() as you describe.
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 6th May 2018 16:48
Is there some limitation on what numbers can be used to represent a group?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 6th May 2018 17:21
I don't believe there is a number limit on groups
but if you are setting the groups is it possible that you are setting them to zero or perhapss a different value to PlayerTankGroup or AITankGroup
it could also be SetSpriteShape. If no shape is set it uses the sprite width and height as a box shape if you are setting a shape it might be the problem
other than that there isn't much else it could be. Perhaps a sprites depth issue if returned the wrong value but doubt it.

Apologies didnt mean to offend sometimes its best when debugging like this to make a small example program if that works it must be in your code
if it doesnt then you have some code people can modify and post a solution.



fubar
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 6th May 2018 17:35
I've reviewed your points, but everything seems to be in order. I will try your suggestion to create a small example program for testing.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 6th May 2018 17:45 Edited at: 6th May 2018 18:25
perhaps before you do you could try and print the values of PlayerTankGroup and AITankGroup
in rhe code that checks collisions its quite possible rhey are borh zero. Values like that I personally
set as a constant or make global


oh wait I just looked at your code again that code will only work if sprite 1 collides and only if its in the following groups try
fubar
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 6th May 2018 20:03 Edited at: 6th May 2018 20:06
are you moving the camera? are you sure x and y are correct with the new coords? if you are moving the camera with setviewoffset or setviewzoom you will need to transform x and y to match the new camera view using ScreenToWorldX( x ) & ScreenToWorldY( y )
do the objects have a shape?
i know you said you checked but you can also try turning physics debug mode on so you can at least make sure the objects have the correct shapes
SetPhysicsDebugOn()
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 7th May 2018 12:29
SMALLG - Yes I am aware of the camera consideration. I'll try Physics Debug
FUBAR - By collision, do you mean SetSpriteCollideBit() for the sprite?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 7th May 2018 13:10 Edited at: 7th May 2018 13:11
your example


when it should be more like



g1 will be the sprite id
fubar
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 7th May 2018 17:31
Sorry, I don't know what you mean by "collision for sprite g1 now".
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 7th May 2018 17:48
Quote: "The code looks like this, inside of a FOR loop:"

your code


with that code it will only continue if spriteId 1 is at x or y location and only if sprite 1
is in PlayerTankGroup or AITankGroup it will never continue otherwise it doesn't matter
what other sprites are at x,y even if they are in the groups. if you change the following
line to if g1>0 or g2>0 then continue it will continue if any sprites in PlayerTankGroup or
AITankGroup are at x,y as getspritehitgroup doesn't return a true or false it returns the
actual sprite id that at x,y restricting it to the groups you have called it with.
ie GetSpriteHitGroup( PlayerTankGroup, x, y ) returns the sprite at x,y of its in playerTankGroup
otherwise it returns zero it may be better to have something like
fubar
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 7th May 2018 18:03
Using g1>0 rather than just g1 in the IF statement doesn't fix this problem (I tried). GetSpriteHitGroup() will return a zero if no hit occurs; also no sprite IDs in my game are ever 0.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 7th May 2018 18:10
There must be something else not right then maybe post more code il have a look after some sleep lol
If the values change in PlayerTankGroup or AITankGroup that might also be an issue but there is no reason
they should be anything but a #constant that never changes in my opinion. but hard to see what you are
trying to achieve and how you are going about it. There are many experienced programmers on here that
if you post some code they will be more willing to help and possible post a perfect sollution.
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 8th May 2018 00:50 Edited at: 8th May 2018 03:12
I made you an example program demonstrating the use of getSpriteHitGroup
try it and pay attention to the text at the top left hand corner it changes
the redTeam scores sometimes while the BlueTeam other times



if you change the line in above code
SetSpriteGroup(blueSpr,group)
to
SetSpriteGroup(blueSpr,someotherGroupvalue)
then blue team will never score

fubar
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 8th May 2018 08:19
Do your sprites have multiple shapes assigned to them? GetSpriteHitGroup will only look at shapeID 1. From the help: "When the sprite is not setup for physics this test will only apply to the base shape at shapeID 1."
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 8th May 2018 12:19
FUBAR - Thanks for the code. Yes groups are constants in the code. I will try to post a more extensive code example.
CJB - No sprite that can occupy a given space has multiple shapes associated with it. I am using dummy sprites to populate a map with different terrain types. Each dummy sprite will have just a shape to occupy each location on the map, and DrawSprite is used to "draw" the terrain on the map.
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 8th May 2018 12:34 Edited at: 8th May 2018 12:35
Below is the routine where the the GetSpriteHitGroup() command is used. This is the top level AI routine for a turn based tank/vehicle strategy game. It breaks down like this:

1 Check if new units can be produced
2 Check for enemy targets
3 Display text
4 For loop that examines each AI unit one at a time:

Check if a unit is alive
Check if the unit is stunned
Set a goal for this turn and setup a goal node (location) for movement
Here is the critical code - check if ANY other unit occupies the goal node - If so, then skip this turn (don't go there)
Check if the unit can fly, if so then fly, else move ground unit

5 Delete Text
6 For loop to check if each unit can be repaired.

CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 9th May 2018 00:10
I'd guess ScreenToWorldX(mapTable[AITank[i].goalNode].x & y aren't quite where they should be. You could maybe place a little marker sprite at that location just to see whether it appears over whatever you are testing for? Keeping in mind that getspritehitgroup will take into account the angle and scale of a sprite. Also, use SetPhysicsDebugOn() to visualise the collision shapes.
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 9th May 2018 01:18
Looks like you're on to something CJB. I setup marker sprites as you suggested. They usually appear in the expected places, but not always. Why do you think ScreenToWorldX(mapTable[AITank[i].goalNode].x & y is in the wrong place?
greycheek
8
Years of Service
User Offline
Joined: 13th Mar 2016
Location:
Posted: 12th May 2018 14:34
It turns out that GetSpriteHitGroup() works for one group but not the other. Strange.

Login to post a reply

Server time is: 2024-03-29 06:33:06
Your offset time is: 2024-03-29 06:33:06