Problem with setAbsolutePosition

Hello all
I am new to this, so it might be a problem with me. I’m implementing a drag and drop by using this PG: https://www.babylonjs-playground.com/#1HTCCO#21 as my base code, but there is problem with it, when I do drag and drop the box for some minutes then it crashes and the theres nothing in the scene with an exception error “Uncaught RangeError: Maximum call stack size exceeded”. I googled it and found out its because of using setAbsolutePosition here:

   var onPointerMove = function (evt) {
    if (!dragPoint) {
        return;
    }

	var pickParentInfo = pickParent(currentMesh);
	if (!pickParentInfo.hit) {
		return;
	}
	
    var current = pickParentInfo.pickedPoint;
	var normal = pickParentInfo.getNormal(true);
	var parentMesh = pickParentInfo.pickedMesh;
	
	if (parentMesh == currentMesh.parent &&
		normal.x == planeNormal.x &&
		normal.y == planeNormal.y &&
		normal.z == planeNormal.z) {
			
		var dragVector = current.subtract(dragPoint);
		currentMesh.position.addInPlace(dragVector);	
	}
	else {
		var displacement = new BABYLON.Vector3(-0.5, -0.5, -0.5); //TODO: calcular esse vetor de acordo com o ponto zero do mesh
		var vector = normal.multiply(displacement);
		var boundingInfo = currentMesh.getBoundingInfo();
		var dimensions = boundingInfo.maximum.subtract(boundingInfo.minimum);
		var diff = dimensions.multiply(vector);
		var absolutePosition = current.subtract(diff);
		
		currentMesh.parent = parentMesh;
		currentMesh.setAbsolutePosition(absolutePosition);
		
		planeNormal = normal;
	}
	
	dragPoint = current;
}

Is there any other alternative or solution for that?

This means you have a method somewhere recursively calling on itself so you end up in a never ending loop.

I can not repro with your playground. Is there anything special to do to repro ?

After a while, the playground throws this:

One i can understand, but three thrown at the same time?..
Might be line 120 and 121 in the playground (setting the parents and setting its absolute position), but this is an uneducated guess.

The isssue is the playground itself. this happens when you are dragging one of the objects (let’s say blue) and move very fast with the mouse that you eventually on top of red. Then the parenting system goes crazy.

2 Likes

https://www.babylonjs-playground.com/#1HTCCO#24

Cause The Monkey King loves you for joining our community.

But now to solve the stack error with setAbsolute.

Reload the page entirely to make sure none of your event Listeners are still bound.

https://www.babylonjs-playground.com/#1HTCCO#25

I believe a correct callstack fixed the problem so I do not think it is a bug.

Thank you for your solution. I checked it but still giving error although it needs more time working on it to give the error, so its an improvement. even using “currentMesh.position = absolutePosition.clone();” instead of “currentMesh.setAbsolutePosition(absolutePosition);” does not remove the error but it changes the behavior.
my question is that is this the right way to have this kind of behavior between two meshes or is there another way without using setAbsoluteposition?

I did the second one with setAbsolute and was not able to recreate the error no matter how fast I clicked dragged and did interactions, how are you triggering the error? Is it when like the mouse is being released not with focus on the canvas or something?

https://www.babylonjs-playground.com/#1HTCCO#25

Make sure it’s a completely fresh page load.

I found a way to make it recreatable.

Drag the objects tword the camera to the bottom of the screen (under the camera basically) and this only works for when you move an object with a child on it. hmmmm, its gotta be in the _syncParentEnabledState for some reason is recursing over itself to many times?

Its kinda hard to make it happen reliably.

Give me a second I might be able to figure this out.

I am actually gonna second this as a bug. Its kinda weird behavior and is seeming to be linked to how the children/parents are being synced.

I tried enabling .alwaysSelectAsActiveMesh = true; on both meshes to disable culling to see if it was the camera frustrum toggling the enable at the wrong time, but that did not seem to do anything.

just grab the blue cube that has a the child red sphere parented to it to the bottom of the screen and start wiggling back and forth is the easiest way to get it to trigger that I have found.

Looking at it I’m starting to think that these two methods are just passing themselves back and forth?

isSynchronized has a part that calls isSynchronizedWithParent() which might return isSynchronized agian and on and on and on perhaps?

I do not think it is the setAbsolutePosition that is the problem and I have checked all the variables I can think of that he is using and none are ever coming back and null or undefined prior to the error drop.

Remove line 120 and no exception is thrown:

https://www.babylonjs-playground.com/#1HTCCO#27

1 Like

From what he is trying to do that would break his system. Let me see if I can find a work around with that knowledge now though.

@mori
https://www.babylonjs-playground.com/#1HTCCO#28

@RaananW, thanks for that catch makes sense now that you pointed that out. Guess its not a bug if you change the call stack it fixes it.

2 Likes

@Pryme8 it works!! :slight_smile: thank you so much :pray:

1 Like