I’m not a linear algebra guy, so when I encounter this kinda stuff I usually just pound on the keyboard until it works. Ran out of ideas though, so had a question about how to do a “lookAt” when moving a object along a curve so the actor is facing the direction it is moving.
I got the basic code for moving along a curve from https://developer.playcanvas.com/en/tutorials/camera-following-a-path. The problem with this approach is that the developer places empty entities in the workspace that represent the nodes of the curve. The entities have position information but they also appear to have rotation information. From what I can glean, the developer is getting the lookat values using the forward vector of the rotated entities. I just have X and Z positions, no rotation information because…
I implemented pathfinder.js. That’s where my curve nodes are coming from. The data looks like:
this.path = [
[ 0, 0 ],
[ 1, 0 ],
[ 2, 0 ],
[ 3, 0 ],
[ 3, 1 ],
[ 3, 2 ],
[ 4, 2 ],
[ 5, 2 ],
[ 5, 3 ],
[ 5, 4 ],
[ 5, 5 ],
[ 5, 6 ],
[ 5, 7 ],
[ 6, 7 ],
[ 7, 7 ],
[ 7, 8 ],
[ 7, 9 ],
[ 8, 9 ],
[ 9, 9 ]
};
From this data I create two curves…this.px and this.py. The code below works, it moves along the two curves and positions the actor appropriately. Anybody have an idea how to set the lookAt value so the actor is looking along the curve?
Map.prototype.update = function(dt) {
//curve movement
this.time += dt;
var percent = this.time / this.duration;
if(this.path.length > 1) {
//set position along the curve
var posX = this.px.value(percent);
var posZ = this.pz.value(percent);
this.actor.setPosition(new pc.Vec3(posX, 0.5, posZ));
this.actor.lookAt(new pc.Vec(???, 0.5, ???));
}
};
(P.S. I also tried just doing lerps instead of curves. That has the benefit of being able to just point at the next node in the list when moving but the overall movement was kinda herky-jerky compared to the curve approach)
Thanks,
JEH