[Solved] How to get UID of collisioned objects?

For questions about using Classic.

Post » Fri Apr 29, 2011 2:28 pm

My goal is to get UID of collision objects (pair).
I found that there are two system condition "On collision(advance)" and "On overlap(advance)" can pick two objects.
I tried "On collision(advance)" but didn't work. "On overlap(advance)" can get object pair sometimes.

Here is my test file.
http://dl.dropbox.com/u/5779181/collision_pair.zip

Thanks.
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Fri Apr 29, 2011 8:12 pm

Your code here does work properly:

[code:3gvj8dlq]+ System: On collision between Green (Pick) and Blue (Pick)
-> Output: Add 1 to 'hitCnt'
-> Output: Set text to Output.Value('hitCnt') & " : (" & Green.UID & "," & Blue.UID & ")"[/code:3gvj8dlq]

The reason that it's not triggering a collision consistently is that the Ball Behavior is performing it's own collision check and bouncing the balls away from each other before the sprites themselves can overlap. On Collision checks for overlap between two sprites, so if they're never actually overlapping then On Collision won't trigger.

Turn off Solid attribute for the balls and you will see that it's working and the UIDs are being picked properly.

The reason that it works sometimes is that it's possible that timedelta is allowing the balls to move slightly closer to each other than they normally would before being bounced away, and on the next frame they are still overlapping and trigger On Collision.

You can get around this collision problem in a few different ways:

1. Don't use Ball Behavior. Physics Behavior can register "collisions" without having sprites overlap, and as a bonus you will get more accurate angle changes when objects collide.

2. Do use Ball Behavior, but try one of the following:
[list:3gvj8dlq]
[*:3gvj8dlq] Turn off Solid for the ball sprites and code in your own bouncing events to change the angles of the balls when they hit each other.[/*:m:3gvj8dlq]
[*:3gvj8dlq] Add a separate, non-solid detector object to the ball (using Containers) that is slightly bigger than the ball. Use that for your On Collision event so that you can pick up the UID of the object the ball is colliding with. Like so: [/*:m:3gvj8dlq][/list:u:3gvj8dlq]


Hope this helps.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun May 01, 2011 1:40 am

Method 2 is a good solution , Thanks.

But I still has some questions,

1. about collision detecting of Ball behavior
[quote:2f52r2sy]it's not triggering a collision consistently is that the Ball Behavior is performing it's own collision check and bouncing the balls away from each other before the sprites[/quote:2f52r2sy]
I can use sprite's "On collision between" event, like that
[code:2f52r2sy]+ ball: On collision between ball and ball
-> output: Add 1 to 'hitCnt'
-> output: Append text output.Value('hitCnt') & " : ( " & ball.UID & " , " & ball.UID & " ) " & newline[/code:2f52r2sy]
It's work (but not enought for my goal). Ball's collision can be detected in this case.

2. how to get second object(objectB) in "is Overlapping (advance)"
[code:2f52r2sy]+ System: Green (Pick) and Blue (Pick) are overlapping[/code:2f52r2sy]
In "is Overlapping (advance)", it can pick two object( objectA= Green, objectB = Blue).
I can't identify objectA and objectB in action if I use the same object in event like that
[code:2f52r2sy]+ System: ball (Pick) and ball (Pick) are overlapping[/code:2f52r2sy]( objectA= ball, objectB = ball)

Here is my test cap file
http://dl.dropbox.com/u/5779181/collision_pair.zip
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Sun May 01, 2011 10:14 am

[quote="rexrainbow":3juau27c]how to get second object(objectB) in "is Overlapping (advance)"
[code:3juau27c]+ System: Green (Pick) and Blue (Pick) are overlapping[/code:3juau27c]
In "is Overlapping (advance)", it can pick two object( objectA= Green, objectB = Blue).
I can't identify objectA and objectB in action if I use the same object in event like that
[code:3juau27c]+ System: ball (Pick) and ball (Pick) are overlapping[/code:3juau27c]( objectA= ball, objectB = ball)

Here is my test cap file
http://dl.dropbox.com/u/5779181/collision_pair.zip[/quote:3juau27c]

If you want to detect collision between two objects, and get the UID of each of those objects separately without using the Family Trick, then you can do so with this method:

http://dl.dropbox.com/u/529356/Construc ... orting.cap

It's a little more complicated, but it doesn't use families or advanced overlapping.

It does use some global and private variables to keep track of which objects are being sorted during collision though. Anyway, I commented it so have a look, and if you have any questions feel free to post them.

Also... there seems to be a bit of a bug where multiple collisions are registering at once for the same two objects colliding. You could add some events to make sure that only one collision is registered to compensate for that. BUT it is getting the UIDs of either colliding object correctly.

Also, the way it's set up right now, the object with the lowest UID will always show up first in the output list. This is due to sub-event #9, which assigns the order in a loop based on UID ascending. You can change the way the balls are selected to make that more random if you need to.

Anyway, hope this helps.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun May 01, 2011 1:26 pm

I think you could also try "pick closest", based on the dimensions of the room.
Like:
+sprite on collision with sprite
->sprite pick closest to 0x, 0y-- add uid to list
->sprite pick closest to 640x, 480y-- add uid to list
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sun May 01, 2011 4:01 pm

Thank you.
Method 2 (add a detector sprite) looks better for detecting multi-pairs. (I'm not sure because of Family Trick.)
Hope there is a more directly way of the advance version.
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Mon May 02, 2011 4:55 am

Hi,
I test "System - Is overlaping (advance)" for multi-pair overlaping. (#4)
http://dl.dropbox.com/u/5779181/collisi ... _test2.zip

Unfortunately, it can detect "one" pair only.

There is an interesting way to get UID of overlaping objects.
In #5 & #6,
[code:1s4ffk1g]+ Blue: Blue overlaps Green
+ System: For each Green
-> output: Add 1 to 'hitCnt'
-> output: Append text output.Value('hitCnt') & " : ( " & Blue.UID & " , " & Green.UID & " )" &newline
[/code:1s4ffk1g]

I put the same object into family "Blue" and "Green"(family trick :D ).
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Mon Apr 15, 2013 12:31 pm

@rexraimbow

Hello! I have met a problem of overlapping, too.
Here is my problem ...
I have a sprite--Poker , there are five instances of Poker and they stand by a line , some part of one instance is overlapping to another . There is an event: if I touch one instance, it should move to another place. But when I touch the area which is just the area where the two Pokers overlap, the two cards both move. I just want the Poker on the top move.
B
5
Posts: 21
Reputation: 498

Post » Mon Apr 15, 2013 2:58 pm

@melonguns
There has a condition which could pick the top one instance.
https://www.scirra.com/manual/131/common-conditionsrexrainbow2013-04-15 14:59:43
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Mon Apr 15, 2013 6:12 pm

@rexrainbow

Thank you! It really works!
I just wrongly mistook used this method before...I was thinking it would choose the topest instance on the layout!
Thank you again!
B
5
Posts: 21
Reputation: 498

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests