jitter, js api: The issue with jit.anim.node worldpos and if statement
Hello, I'm begging for help, no matter which way I try it doesn't work.
My goal is to make a gridshape move from one coordinate to another, and when it reaches the destination coordinates, it deletes itself
There aren't any errors in logs, but the node's gridshape just continues moving, passing the endCoord and doesn't stop and deleting itself when it's worldpos equals or more than it's route destination point, which is weird because at some moment, the world position of the node is clearly more than endCoord, but the if statement in queryWorldPos() just doesn't execute, why?
function start(startCoord, endCoord, speed) {
function queryWorldPos() {
if (animNode && endCoord) {
var pos = animNode.worldpos;
post("Current world position:", pos[0], pos[1], pos[2], "\n");
// Doesn't work!
if (pos[0] >= endCoord[0] && pos[1] >= endCoord[1] && pos[2] >= endCoord[2]) {
post("\nReached endCoord");
// Clean up
gridshape.freepeer();
animDrive.freepeer();
animNode.freepeer();
if (queryTask) {
queryTask.cancel();
queryTask = null;
}
post("\nCleaned up all objects and stopped querying.\n");
}
}
}
post("Calling animateImpulse with:", startCoord, "to", endCoord, "speed:", speed, "\n");
const gridshape = new JitterObject("jit.gl.gridshape", contextName);
gridshape.shape = "cone";
gridshape.color = [Math.random(), Math.random(), Math.random()];
post("Created gridshape");
const animNode = new JitterObject("jit.anim.node");
gridshape.anim = animNode.name;
animNode.movemode = "local";
animNode.scale = impulseScale;
animNode.position = startCoord;
animNode.lookat = endCoord;
animNode.direction = endCoord;
post("Created jit.anim.node");
const animDrive = new JitterObject("jit.anim.drive");
animDrive.targetname = animNode.name;
post("Created jit.anim.drive\n");
animDrive.move(0, 0, speed);
post("\nMoving node")
// Start querying worldpos
if (!queryTask) {
queryTask = new Task(queryWorldPos, this);
queryTask.interval = worldposQueryInterval;
queryTask.repeat();
}
if (animNode.worldpos[0] > endCoord[0] &&
animNode.worldpos[1] > endCoord[1] &&
animNode.worldpos[2] > endCoord[2]) {
post("\nreached endCoord");
gridshape.freepeer();
animPath.freepeer();
animNode.freepeer();
post("Cleaning and resolving")
}
}
Could you provide a patch that works with this js code? If you've Max 9, you can @embed the code in the patch so you don't need to provide it separately.
Here you are
Your math is wrong. If you print your conditions (pos[0] >= endCoord[0], pos[1] >= endCoord[1], pos[2] >= endCoord[2]
) for deleting the arrow, you get that (see the last three numbers:
Indeed, given that your start position is (0.1, 0.4, -1) and your end position is (0.2, -0.4, 0.2), the Y start coordinate is bigger than the Y end coordinate from the beginning, and gets smaller than it when passed that point. Your 3 conditions are never reached and the object is never deleted.
A better approach is to use relative vectors (not sure if it's the correct term). Given that you want to go from point A to point B and your current position is P, your "goal" vector is B-A, and your current position vector is P-A. You reach your destination once length(P-A) gets bigger than length(B-A).
Another problem you had is the way you defined your task. I believe the function called in a task needs to be at the root level of the code, and so you need to pass it any object you're about to mess with. Other weird thing with this is that you need to pass these objects as an array of objects, as said in the doc (although the examples aren't very explicit).
Here is your example with all my changes:
(I did super quick and ugly changes, you can most certainly refactor this in a proper way, for example by defining a vector class, with vector math methods.
Just for the record, a no JS solution, using 'moveto' and @evalreport 1: