Jump to content

AddContactLeaveCallback never executed


Recommended Posts

Hi,

i am currently facing an issue with callbacks in the physics system, while reacting on collisions. I tried the current setup in latest engine version (2.16.1):

Spoiler
class AppWorldLogic : WorldLogic
	{
		// World logic, it takes effect only when the world is loaded.
		// These methods are called right after corresponding world script's (UnigineScript) methods.
        private ObjectMeshDynamic mesh1;
		private ObjectMeshDynamic mesh2;

        private float deltaT = 1.0f;
		private bool scaleUp = false;
		public AppWorldLogic()
		{
		}

		public override bool Init()
		{
			// Write here code to be called on world initialization: initialize resources for your world scene during the world start.
            mesh1 = Primitives.CreateSphere(1.0f);
			mesh2 = Primitives.CreateSphere(1.0f);

			BodyRigid rigid1 = new BodyRigid();
			ShapeSphere sphere1 = new ShapeSphere(rigid1,1.0f);
            sphere1.Mass = 0.0f;
            sphere1.CollisionMask = 8;
            rigid1.AddContactEnterCallback(OnCollisionEnter);
            rigid1.AddContactLeaveCallback(OnCollisionLeave);
            mesh1.Body = rigid1;

			BodyRigid rigid2 = new BodyRigid();
			ShapeSphere sphere2 = new ShapeSphere(rigid2,1.0f);
			sphere2.Mass = 0.0f;
			sphere2.CollisionMask = 8;
			mesh2.Body = rigid2;

            mesh1.WorldPosition = new vec3(0.0f, -5.0f, 1.0f);
            mesh2.WorldPosition = new vec3(0.0f, 5.0f, 1.0f);


			return true;
		}

		// start of the main loop
		public override bool Update()
		{

            deltaT += Game.IFps;


            mesh1.WorldPosition = new vec3(0.0f, -5.0f * Math.Sin(deltaT), 1.0f);
            mesh2.WorldPosition = new vec3(0.0f, -5.0f * Math.Cos(deltaT), 1.0f);

// Write here code to be called before updating each render frame: specify all graphics-related functions you want to be called every frame while your application executes.


            return true;
		}

		public override bool PostUpdate()
		{
			// The engine calls this function after updating each render frame: correct behavior after the state of the node has been updated.

			return true;
		}

		public override bool UpdatePhysics()
		{
			// Write here code to be called before updating each physics frame: control physics in your application and put non-rendering calculations.
			// The engine calls UpdatePhysics() with the fixed rate (60 times per second by default) regardless of the FPS value.
			// WARNING: do not create, delete or change transformations of nodes here, because rendering is already in progress.

			return true;
		}
		// end of the main loop

		public override bool Shutdown()
		{
			// Write here code to be called on world shutdown: delete resources that were created during world script execution to avoid memory leaks.

			return true;
		}

		public override bool Save(Stream stream)
		{
			// Write here code to be called when the world is saving its state (i.e. state_save is called): save custom user data to a file.

			return true;
		}

		public override bool Restore(Stream stream)
		{
			// Write here code to be called when the world is restoring its state (i.e. state_restore is called): restore custom user data to a file here.

			return true;
		}

        private void OnCollisionEnter(Body body, int num)
        {

        }

        private void OnCollisionLeave(Body body, int num)
        {

        }

 

The OnCollisionEnter()-callback will be called in the exact way I want, but OnCollisionLeave() is never been executed. Am I doing something wrong?

Thanks

Christian

Edited by christian.wolf2
Link to comment

Hi Christian,

The main issue here is changing positions of the objects by setting their positions directly (via WorldPosition), which is not expected for physical objects.

You need to use forces and impulses in order to do physical objects interactions.

Please check the modified version of your code in the attachment:

AppWorldLogic.cs

Or you simply can use built-in Physical Trigger.

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment

Hi silent,

thanks for the reply and thanks for the updated code, which only partially solves my problem. The main feature I want to add is having an (invisible) sphere around the user, that will follow him directly. Using the C#-shooter example solves this problem.

However, in our world we have some tools that can be equipped/unequipped to the user. Those tools also has an invisible sphere. When the users physical sphere enters contact with the tools-sphere, we want to show some information above the tool that it can be equipped. When the users-sphere leaves the contact, we want to hide the information.

So the ultimate goal is to have some invisible physical spheres that do the collision detection in the physics thread but they DO NOT do any additional physical simulation (friction, kinetic forces on each other) in order to not move the normally rendered tools. That's why we set the mass to 0 in the previous example. Can this be easily be done with the standard physics model?

Because BodyDummy aren't doing any collision detection and callbacks, this was our first guess to go with. The second way was to have a physical trigger and do the updateContacts manually each physics call.

Best

Christian

Link to comment
×
×
  • Create New...