Archive 17/01/2023.

Collision detect problem

rogerdv

I have set up a node as a trigger to detect when the player reaches the area. I create a CollisionShape component, expand its size in X and Y to cover the desired area, then add a RigidBody, set its Collision Event Mode to Always and enable Trigger checkbox. Also added an StaticModel (the box), just in case, and make it invisible by unchecking all the View mask checkboxes.
In the player creation code I have this:

node = scn.CreateChild(id); model = node.CreateComponent("AnimatedModel"); body = node.CreateComponent("RigidBody"); shape = node.CreateComponent("CollisionShape"); shape.model = model.model;

In the gameplay code I subscribe collision event and set up a basic handler:

SubscribeToEvent("NodeCollision", "HandleTriggers");
***
void HandleTriggers(StringHash eventType, VariantMap& eventData)
	{
		Node@ n = eventData["OtherNode"].GetPtr();
		Print("collided with "+n.name );
	}

But when I make the player model walk to the area, nothing happens. Is there any missing step in the shape and body creation?

codingmonkey

Also added an StaticModel (the box), just in case, and make it invisible by unchecking all the View mask checkboxes.
make your cube visible, and check is it on same place as before in editor. may be it’s gone somehow, no ?

also read this urho3d.github.io/documentation/1 … ysics.html
"An example of reading collision event and contact point data in script, from NinjaSnowWar game object collision handling code:"

rogerdv

The cube is in the right place, I can make the character walk trhough it. Also tried enlarging the collisionShape, but doesnt works.

codingmonkey

may be you forgot node parameter ?

SubscribeToEvent(node, “NodeCollision”, “HandleNodeCollision”);

are you using like in ninga classes hierarchy?

[spoiler][ul]class GameObject : ScriptObject ?
{
void HandleNodeCollision(StringHash eventType, VariantMap& eventData)
{
…[b]WorldCollision/b;
}

void WorldCollision(VariantMap& eventData){…}
}

class Ninja : GameObject
{
void Start()
{
SubscribeToEvent(node, “NodeCollision”, “HandleNodeCollision”);
}
void WorldCollision(VariantMap& eventData)
{

}
{

}
}[/ul][/spoiler]

rogerdv

I looked at ninja as guide, but I skipped the node parameter because I thought it was optional and that the event handler would catch all collisions. Will try with that.

rogerdv

Well, this is what I do:

Array<Node@> snodes = gameScene.GetChildren(); Print(snodes.length); for (int i=0;i<snodes.length;i++) { if (snodes[i].name=="emarker") { //Found an entity marker Variant val = snodes[i].vars["entity"]; snodes[i].name=val.ToString(); ent.CreateFromScene(gameScene, snodes[i],val.ToString()); } else if (snodes[i].name.Contains("trig")) { //found a trigger SubscribeToEvent(snodes[i], "NodeCollision", "HandleTriggers"); }//if }//for

It finds the trigger node, but no event is called.