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.

Newcomers AppGameKit Corner / [SOLVED] 2D - How to detect collisions between overlapping sprites ?

Author
Message
patmaba
16
Years of Service
User Offline
Joined: 4th Sep 2002
Location: Belgium
Posted: 6th Dec 2018 18:22
Hello,

How to detect collisions between overlapping sprites ?

The source code below has three sprites.

- The blue element represents the sea
- The green corresponds to the land
- The red, the hero.

If I place the red hero on the grass. The collision with the sea also occurs when it is not.

How to detect the collision with the grass only without the see collision ?
How to detect the collision with the sea by taking into account the overlapping images ?

I need to know when the player(red sprite) touch water only ?

Thanks



Sincerely

Patmaba

The author of this post has marked a post as an answer.

Go to answer

puzzler2018
1
Years of Service
User Offline
Joined: 8th Oct 2017
Location:
Posted: 6th Dec 2018 18:50
ummm Interesting, I would stick a few IF conditions in there



Unless can use some kind of SetSpriteGroup and GetSpriteHit commands
patmaba
16
Years of Service
User Offline
Joined: 4th Sep 2002
Location: Belgium
Posted: 6th Dec 2018 20:59
Thank you for your draft code.

First remark the following condition is useless because it is always true.


You give me part of the answer to the question:
How to detect the collision with the grass only without the see collision?

For the second part, it is incomplete.
Imagine that half of our hero's body is on the ground and the other half of his body has his feet wet.
In this case, the functions must return the information for the 2 collisions.

It pisses me off. I haven't found a solution.
puzzler2018
1
Years of Service
User Offline
Joined: 8th Oct 2017
Location:
Posted: 6th Dec 2018 21:14 Edited at: 6th Dec 2018 21:14
The only way could test that scenario is combine sprites together.

A body sprite
and a
A foot sprite

which are joined together by simply moving together in a scene and then you will have a feet in the water collision
puzzler2018
1
Years of Service
User Offline
Joined: 8th Oct 2017
Location:
Posted: 6th Dec 2018 21:21 Edited at: 6th Dec 2018 21:23
Like this



Virtual Nomad
13
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 7th Dec 2018 08:01 Edited at: 7th Dec 2018 08:11
in the end, i think you simply want to know if the entire hero sprite is completely contained within the land sprite?

assuming your sprites are all (non-rotated) box shapes as your code suggests, i'd check opposing corners of the hero and compare the results (note the 2 1px "dummy" sprites added to your code):


in theory, this should work, but i can see that if either of the corners are (visibly) 1 pixel "outside" of the land sprite, it still records collision. i'm assuming its due to virtual resolution?


otherwise, you could try a more-manual approach and remove AppGameKit sprite collision from the equation altogether and consider checking if the 2 points both lie within the rectangle that is the land (mathematically).

(or simply add a 1px transparent border around your hero sprite to account for the visible anomaly produced by the above "solution")

i hope that helps!

Attachments

Login to view attachments
patmaba
16
Years of Service
User Offline
Joined: 4th Sep 2002
Location: Belgium
Posted: 7th Dec 2018 14:52
Thank you Virtual Nomad for your draft source code .

Some remarks about your sprite dummy. It is no longer necessary to do this :


You can do this directly with the new function CreateDummySprite

I like your 1px transparent technique.

I refined my collision detection technique by using this other function: GetSpriteHit

I get what I want here is the code:




Thanks to all

patmaba







Virtual Nomad
13
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 7th Dec 2018 15:47
This post has been marked by the post author as the answer.
i haven't played with AppGameKit sprites much and didn't notice CreateDummySprite(); nice to know. we both learned something

Login to post a reply

Server time is: 2018-12-19 00:31:55
Your offset time is: 2018-12-19 00:31:55