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 / sprite collision detection with any other sprite

Author
Message
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 13:24
Hi can anyone advise?

I'm more used to using DBPro which has a command that could detect a collision between sprite1 and sprite2, but sprite2 could be replaced with 0 to detect a collision between sprite1 and any other sprite.


Is there an equivalent in AppGameKit to detect a collision between sprite1 and any other sprite or would I need to put sprite2 in a for..next loop which doesn't seem very efficient if there are a lot of other sprites.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Oct 2013 14:16 Edited at: 16th Oct 2013 14:18
If they are physics sprites you can use GetFirstContact(), loop through the existing contacts using GetSpriteNextContact() and check for an actual collision with each "contact". Contacts are not necessarily touching so an additional collision check is needed.

You could turn them into physics sprites even if it's not needed for the game itself, just set them as kinematic and as sensors and they won't react to any physics in your game or cause any physics reactions.

Edit: Try using getSpriteContact(spriteID,-1) first, you never know

oct(31) = dec(25)
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 15:14 Edited at: 16th Oct 2013 16:19
Hi baxslash.

I am trying to convert an old DBPro card game to AGK. It doesn't use physics sprites.
I can move 1 card using mouse or touch but I then need to detect a collision when that card touches another on completion of the movement.

My understanding of using contacts is when 2 or more collisions occur with sprite1 simultaneously or at the same time during prolonged contact. I don't believe it can be used to detect a single collision of sprite1 with any other sprite.

I also tried using getSpriteContact(spriteID,-1) but unfortunately it isn't recognised, nice idea though.


Edit: n-p, I have decided to re-write part using arrays instead of collision.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Oct 2013 16:59 Edited at: 16th Oct 2013 17:00
Quote: "My understanding of using contacts is when 2 or more collisions occur with sprite1 simultaneously or at the same time during prolonged contact. I don't believe it can be used to detect a single collision of sprite1 with any other sprite."

All you are waiting for is one collision with any other sprite so you could just check whether any contacts exist and if so whether a collision has occurred. You can easily use physics sprites to get this with very little effort. Just because you aren't using physics doesn't mean you can't...


oct(31) = dec(25)
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 17:12
The sprite contact commands only work for sprites that have physics set on them.

But you can use the hit commands on non-physics sprites. GetSpriteHit(x,y) will return the first sprite found at that point. But that probably won't do what is desired in this post.

The contact commands work fairly well to detect any contacts. As long as you do the checks between Sync() calls, any contact will be detected.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Oct 2013 17:37 Edited at: 17th Oct 2013 15:19
without physics.
so?

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 18:02
I forgot about the GetSpriteCollision command. But it doesn't let you check to see if any sprite has had a collision. It requires specific sprite ids.

I think Juney's purpose was to get a generic 'was anything hit' and then if so, 'what was hit'. Those options are only available on sprites with physics.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Oct 2013 18:15
What I'm saying is "Why not just use physics?". Just because you don't want things bouncing around doesn't mean you can't use physics.

Just use these commands on your card sprites before using my function to see if any collisions have occurred on the specified sprite:
setSpritePhysicsOn(spriteID,3)
setSpritePhysicsIsSensor(spriteID,1)

Job done!

oct(31) = dec(25)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Oct 2013 18:30
@baxslash
for a card game the defaulf rect shape should be enough .
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Oct 2013 18:43
Quote: "for a card game the defaulf rect shape should be enough ."

I'm not changing the shape

oct(31) = dec(25)
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 19:10
Thank you all for supplying your answers.
I have had a quick go with what has been suggested but have either been unable to get it to work for me, or simply producing errors.
I think I need a bit more time to work through your suggestions more thoroughly to see if I can get them to work, so please bear with me.
Thanks again.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 20:00
Not all DB Pro code translates exactly to AGK.

What sort of errors are you seeing?

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 20:54
Hi A_Lady

I'm finding out the hard way that it is not necessarily an easy conversion from DBP to AppGameKit, but with some tweaks the algorithms should be similar.

In trying the function by baxslash, the 1st error was not to allow the function name but that is easily rectified.
The next error is proving more difficult to overcome : "Sprite 0 does not exist at line 264".
I am currently using sprites 1-127 with more to add.
I created a sprite 0 using :



I was not able to create sprite 0 using an image so just left it blank which creates a tiny white square in the top left corner.

However I still get the same error box from within the function.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 20:57
The command 'CreateSprite(0)' does not create a sprite with id 0, it creates a sprite using image id 0. And this makes a blank square sprite, default size 10x10, as you discovered.

You cannot create anything with id=0. All ids must be 1 or greater.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 21:04
Quote: "You cannot create anything with id=0"


Can you explain the error box "Sprite 0 does not exist at line 264".

I tried adding lines : If spriteID > 0 but this prevents baxslash's code from doing anything.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 21:34
Quote: "Can you explain the error box "Sprite 0 does not exist at line 264"."

That is the error message that you get when you try to do something with a nonexistent sprite. If you provide a sprite id in the command, it feeds that id back to you.

How are you using 'If spriteID > 0' with respect to the function that baxslash put up? The function itself looks mostly okay. Except I would use a while loop instead of a repeat one. The repeat loop will always execute at least once. A while loop would not execute (in this function) if the first call to getSpriteFirstContact produces zero.

I would adjust the code to look like this:


Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 16th Oct 2013 22:42
Thanks again A_Lady.
I have worked out why it errors "Sprite 0 does not exist" on this occasion.

When the cards are 1st dealt out no card has been selected thereby passing a spriteID = 0. The line "c = getSpriteFirstContact(spriteID)" causes this error. This occurs in your coding as well.
However this can be dealt with by adding If spriteID > 0 before calling the new function in the main loop.

On to the next query.
The line "spriteID2 = getSpriteContactSpriteID2()" always returns a 0 instead of a sprite number in contact with the 1st spriteID, i.e spriteID2 always returns as 0.
This once again brings up the error "Sprite 0 does not exist"

I'm not convinced this command works.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Oct 2013 23:32
Quote: "This occurs in your coding as well."

As would be expected because the value passed into GetSpriteFirstContact is zero (which is the error).

As for getSpriteContactSpriteID2() always returning zero, that might require more investigation and more of the code you are using (like the setup of all the sprites).

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th Oct 2013 12:53 Edited at: 17th Oct 2013 12:55
It is possible that you are detecting a collision with the edges of the screen (or walls). Use:
SetPhysicsWallLeft(0)
SetPhysicsWallRight(0)
SetPhysicsWallTop(0)
SetPhysicsWallBottom(0)

Before your other code. That might fix the spriteID2 error, if not it's likely to be another coding error. I have used this system before successfully.

oct(31) = dec(25)
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 17th Oct 2013 14:34
Thanks but no success.
Clearly now there must be another coding error as you suggested. I will try to identify this but at the moment I think I am more tempted to discard collision detection and write a totally alternative method.

Thanks again, I will post again if I find the error(s).

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Oct 2013 15:18 Edited at: 17th Oct 2013 15:21
did you try the example from me above?
its just a array with card sprites to compare if they interleaved.
you can also put a user defined type in a array with more card informations.
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 17th Oct 2013 16:31
Hi Markus, apologies for not fully trying your example.
I was a little put off from delving too deep because I was concerned about using a for..next loop especially when I can have 200+ cards.

However I implemented your code, changed a few bits and hey, it works perfectly as well as providing the necessary card id it rests on.

Many thanks. I will use it.

Login to post a reply

Server time is: 2024-11-24 22:37:40
Your offset time is: 2024-11-24 22:37:40