It’s been a day since I’m trying to recreate Bezier Curve in playcanvas I’ve tried so many ways but can’t make it
The only thing I could make is smoothly move game object to clicked object so it would be nice if someone could give me a hint to make this code done
I’ve been only using csharp and unity so far, I’ve only been using playcanvas for the first day, so it was pretty hard to get used to JS
var MovementController = pc.createScript('movementController');
MovementController.attributes.add('playerEntity', {type: 'entity', title: 'Player Entity'});
MovementController.attributes.add('desiredTime', {type: 'number', default: 0, title: 'Desired Time'});
MovementController.attributes.add('numberOfPoints', {type: 'number', default: 0, title: 'Number Of Points'});
MovementController.attributes.add('entityCamera', {type: 'entity', title: 'Camera'});
MovementController.tempVec3 = new pc.Vec3();
MovementController.startPosition = new pc.Vec3();
MovementController.tempEntity = new pc.Entity();
MovementController.closestEntity = new pc.Entity();
MovementController.prototype.initialize = function()
{
this.pointsArray = this.app.root.findByTag("Points");
this.elapsedTime = 0;
this.percentageComplete = 0;
this.distance = 0;
this.startPosition = this.entity.getPosition();
this.currentDist = 0;
this.startDist = 0;
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
this.on('destroy', function() {
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onSelect, this);
}, this);
var tempPos = this.entity.getPosition();
this.pointsArray[0].setPosition(tempPos);
console.log("start method");
};
MovementController.prototype.update = function(dt)
{
if(typeof this.tempEntity == 'undefined')
return;
if(typeof this.closestEntity == 'undefined')
{
console.log("once");
this.closestEntity = this.findClosestCube(this.tempEntity);
}
this.elapsedTime += dt;
this.numberOfPoints += dt;
this.percentageComplete = clampBetween01(this.elapsedTime)/this.desiredTime;
if(this.app.keyboard.wasPressed(pc.KEY_SPACE))
{
this.tempEntity = this.pointsArray[0];
}
this.currentDist = new pc.Vec3().distance(this.startPosition, this.tempEntity.getPosition());
this.tempVec3 = calculateQuadraticBezierPoint(this.startPosition, this.closestEntity.getPosition(), this.tempEntity.getPosition(), this.numberOfPoints);
var lerpedRotation = new pc.Quat().slerp(this.entity.getRotation(), this.tempEntity.getRotation(), this.percentageComplete);
this.distance = new pc.Vec3().sub2(this.entity.getPosition(), this.tempVec3).length();
this.entity.setRotation(lerpedRotation);
this.entity.setPosition(this.tempVec3);
if(this.distance < 0.0001)
{
var tempPos = this.tempVec3;
var tempQuat = this.tempEntity.getRotation();
this.entity.setPosition(tempPos);
this.entity.setRotation(tempQuat);
this.startPosition = tempPos;
}
};
MovementController.prototype.findClosestCube = function(cube)
{
var closest = null;
var distance = 1000;
for (var i = 0 ; i< this.pointsArray.length ; i++)
{
var diff = this.pointsArray[i].getPosition().sub(cube.getPosition());
var curDistance = diff.lengthSq();
if (curDistance < distance && this.pointsArray[i] != this.tempEntity)
{
closest = this.pointsArray[i];
distance = curDistance;
}
}
console.debug(closest.name);
this.currentDist = new pc.Vec3().distance(this.startPosition, this.tempEntity.getPosition());
this.startDist = this.currentDist;
this.numberOfPoints = this.currentDist/this.startDist;
return closest;
};
MovementController.prototype.onSelect = function (e)
{
var from = this.entityCamera.camera.screenToWorld(e.x, e.y, this.entityCamera.camera.nearClip);
var to = this.entityCamera.camera.screenToWorld(e.x, e.y, this.entityCamera.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result)
{
this.tempEntity = result.entity;
this.startPosition = this.entity.getPosition();
}
};
also this is bezier code
var BezierCurve = pc.createScript('bezierCurve');
// initialize code called once per entity
BezierCurve.prototype.initialize = function()
{
};
function calculateLinearBezierPoint(point1, point2, time)
{
return point1 + time * (point2 - point1);
}
function calculateQuadraticBezierPoint(point1, point2, point3, t)
{
var u = 1 - t;
var tt = t * t;
var uu = u * u;
var bPoint = new pc.Vec3(point1.x * uu, point1.y * uu, point1.z * uu);
bPoint.add(new pc.Vec3(2 * u * t * point2.x, 2 * u * t * point2.y, 2 * u * t * point2.z));
bPoint.add(new pc.Vec3(point3.x * tt, point3.y * tt, point3.z * tt));
return bPoint;
}