I am making a little game using playcanvas in a web browser. I build it in javascript, and I am trying to add collisions. However, they seem to not be implemented.
I defined two entities : a cube and a ball, where you control the ball. I am trying to detect when the ball touches the cube. I defined them like this :
// Define the cube here
const cube = new pc.Entity("cube");
cube.addComponent("model", {
type: "box"
});
cube.addComponent("collision", {
type: "box",
scale: cube.getLocalScale().divScalar(2)
});
cube.addComponent("rigidbody", {
type: "static",
friction: 0.5,
restitution: 0.2
});
cube.on("collisionstart", (result) => {
console.log(result) // Just to see what it is
});
// Define the ball here
const ball = new pc.Entity("ball");
ball.addComponent("model", {
type: "sphere",
castShadows: true,
receiveShadows: true
});
ball.setLocalScale(0.2, 0.2, 0.2);
ball.setPosition(-5, 0, 0);
ball.addComponent("collision", {
type: "sphere"
});
ball.addComponent("rigidbody", {
type: "dynamic"
});
ball.on("collisionstart", (result) => {
console.log(result) // Just to see what it is
});
I use key inputs in the app.on("update")
event to move the ball with the ball.translate
method, with these methods :
const forward = (delta) => {
const forward = ball.forward;
ball.translate(0, 0, forward.z * delta);
}
const backward = (delta) => {
const forward = ball.forward;
ball.translate(0, 0, -forward.z * delta);
}
const left = (delta) => {
const right = ball.right;
ball.translate(-right.x * delta, 0, 0);
}
const right = (delta) => {
const right = ball.right;
ball.translate(right.x * delta, 0, 0);
}
I searched for event to add to the entities, I tried a bunch of them (collisionstart
for example), but they logged nothing.
Did I do something wrong or do I miss something?