How to check the objectCollision that send message? (SOLVED)

Hello guys… help me with this question.

My GO has two object colision. one at top another at bottom. when i use pprint to check the sent value I got

[main:/instance1#collisionobject]

so i need to compare the values, I do this:

if sender == hash(<????>) then ... end

in this case which value i need to put in <????> to check wich component receive the colission?

It is an URL so if sender.path == hash("/instance1") should work.

2 Likes

but this is the problem…

“/instance1"
”/instance1"
"…"
"/instance10"

is is automatically generated by the factory

how to handle with is?

“/instance??#collisionobjectTOP
"/instance??#collisionobjectBOTTOM"

Hello @jhonatanvinicius,

Remember that all objects can send and receive messages, so in your case, your player is receiving a collision message in the format that you are describing: “/instance??#collisionobjectTOP”, BUT your other object can also detect the collision and send a message identifying itself, something like (pseudocode):

other object on_message(...)
  if message == contact_point
     msg(player_url, "I_GOT_YOU", { id = go.get_id() } )

And then your player object would have to listen for these messages:

player object on_message(...)
  if message == "I_GOT_YOU"
     receive_damage_from(message.other_id)

OR

Another case would be to use only the fragment portion from the sender (sender is an url) so you get the collision that was hit “#collisionobjectTOP” or “#collisionobjectBOTTOM” without taking in consideration who sent it:

player object on_message(...)
   if message == contact_point
      if sender.fragment == #collisionobjectTOP then
            -- receive top damage
      else if sender.fragment == #collisionobjectBOTTOM then
            -- receive bottom damage
      end

Hope this helps

3 Likes

wow i believe that now will work. This option(sender.fragment) it is new for me. when at home i will test. thanks.

Nice,

You see, url’s have 3 parts: socket, path and fragment.

You can read more about it in the defold msg.url documentation

2 Likes

i see. but i cant check the right contact object… see the image bellow:

here is the contact point:

Ok,

I think it is pretty easy, you have to do something like (pseudocode):

player object on_message(...)
   if message_id == contact_point
      if sender = url("footgirl")
          -- receive foot damage here

Might find this helpful too,

if message_id == hash("contact_point_response") then	-- check if we received a contact point message
	if message.group == hash("foot_girl") then		-- check that the object is something we consider geometry
		handle_geometry_contact(self, message.normal, message.distance)
	end
end

and change it’s group to foot_girl

1 Like

Hey @Davej ,

Yours is a good idea too, to use:

message.group == hash("foot_girl")

instead of:

sender == msg.url("foot_girl")

but @jhonatanvinicius would have to change his game objects and collision objects, doesn’t he?

HEllo @Edgaronfo and @Davej.

I have tst your hints but didn’t work…

see my test details…

  1. in my dino script i put this inside message function:
if message_id == hash("contact_point_response") then

       if message.group == hash("hero") then
	 		pprint("+++++++ begin test")
	 		pprint("footgirl as url: " .. msg.url("footgirl"))
	 		pprint("message sender: " .. sender)
	 		pprint("message id: " .. message_id)
	 		if sender == msg.url("footgirl") then -- check edga hint
	 			print('OK to URL')
	 		
	 		end
	 		
	 		if message.group == hash("footgirl") then -- check dajev hint
	 			print('OK to group')
	 		
	 		end
	 		pprint("   === start message info")
	 			  pprint(message)
	 		pprint("   === end message info")
	 		pprint("+++++++ end test")
      end
end

and this is my console response:

DEBUG:SCRIPT: +++++++ begin test
DEBUG:SCRIPT: footgirl as url: [main:/footgirl]
DEBUG:SCRIPT: message sender: [main:/instance1#collisionobject]
DEBUG:SCRIPT: message id: [contact_point_response]
DEBUG:SCRIPT:    === start message info
DEBUG:SCRIPT: 
{
  normal = vmath.vector3(0, -1, 0),
  position = vmath.vector3(220.70994567871, 70.166679382324, 0),
  other_position = vmath.vector3(237.7099609375, 120.5647354126, 0.30000001192093),
  relative_velocity = vmath.vector3(0, 0, 0),
  other_id = hash: [/hero],
  other_mass = 0,
  group = hash: [hero],
  applied_impulse = 0,
  distance = -0.66667795181274,
  life_time = 0,
  mass = 0,
}

DEBUG:SCRIPT:    === end message info
DEBUG:SCRIPT: +++++++ end test

my hero GO

my dino go

any ideas?

Hello my friend! I don’t know if I really understand what do you want to do, but I have this piece of code that helps me a lot…

I put this inside the script attached to the factory, in your case, I think it’s the dino script:

this_id = tostring(go.get_id())
this_id = this_id:sub(8, -2)--hash: [/]

Sorry if isn’t what you are looking for…

2 Likes

I wouldn’t rely on string operations, since the strings won’t be available in release builds.

1 Like

Hi @jhonatanvinicius!

A few things:

  1. I think you have a typo: In your screenshot, the collision object is called “foot_girl”, but in your code, you have written “footgirl”
  2. I would enable the debug physics rendering to see that the feet actually are where you think they are.
1 Like

You need to change the group of the hero collision object foot_girl to foot_girl, if you want to check different collisions objects on the same game object, they must belong to different groups, else all collisions will be checked under the same group

1 Like

Yes @Edgaronfo, thats right, but at least then you can check for a specific collision, if he wants to still use that area under hero, he can always place another collision object int the same area and call it hero still.

1 Like

Ok,

In this specific case, your dino is checking for the hero, and NOT the hero for the dino, so the collisions that will be sent as sender will be you dino’s, that is why you receive “collisionobject” as your sender.

So if you want to know if you hero’s feet got bitten, you have to put code in your hero’s script, then you will be able to receive the hero’s collisions as senders.

Try this in your hero.script:

if message_id == hash("contact_point_response") then
       if message.group == hash("dino") then
	 		print("message sender: " .. sender)

and your result will be something like:
DEBUG:SCRIPT: message sender: [main:/hero#foot_girl]

3 Likes

yeah it is working now!!!

you are right! @Edgaronfo.

I have learned a important thing about collisionObject today. here is may error:

I was trying “teach” to dino when he should die… but defolt dont tell to receiver the id of other collision object. basically a GO/script has access only to own object collisions.

Now this is my information path:
1 - in girl i check if sensor foot make contact with dino…
2 - so i send a message to dino with command “die”
3 - the dino receive the command and send a return message to girl telling his coordinates Y and starts his die animation
4 - the girl receives the message with coordinate and start to jump from dino’s head.

Really thanks for all help, guys.

5 Likes