How to get the coordinates of an contact point?

Like this.entity.getPosition(), is there a method to get the coordinates (x, y, z) of a contact point of an entity with another?

Hi, first add rigid body and collision components on both entities, create script and add script to that entity which is moving

var Collider = pc.createScript('collider');

// initialize code called once per entity
Collider.prototype.initialize = function () {
    this.entity.collision.on('contact ', this.onContact , this);
};

Collider.prototype.onContact = function (result) {
console.log(result); // here you get all the details of the other body.
};

but the exactly contact point, because when I run the result I can get the position of the other entity, I don’t found how to get the point where the collision occurs. I am tryng to get the collision point of an ball in a ramp

Hi @Joao_Pedro_Kuhn!

I think it should something like below, but I’m not sure.

var contactPosition = result.contacts[0];
1 Like

Thanks you all, I’ve solved the problem in another way:
var LineVectors = pc.createScript(‘lineVectors’)

var LineVectors = pc.createScript('lineVectors')

// initialize code called once per entity
LineVectors.prototype.initialize = function () {
    this.worldLayer = this.app.scene.layers.getLayerById(pc.LAYERID_WORLD)
    this.entity.collision.on('contact', this.onContact, this)
    this.contactVector = new pc.Vec3()
}

LineVectors.prototype.onContact = function (result) {
    if (result.other instanceof pc.Entity && result.other.name === 'Ramp Model') {

        this.forcaNormal = this.entity.getPosition().clone()
        this.forcaNormal.y += Math.cos(toRadians(angulo)) * this.peso
        this.forcaNormal.z += Math.sin(toRadians(angulo)) * this.peso
        this.app.drawLine(this.position, this.forcaNormal, pc.Color.GREEN, false, this.worldLayer)

    } else if (result.other instanceof pc.Entity && result.other.name === 'Plane') {

        this.forcaNormal = this.entity.getPosition().clone()
        this.forcaNormal.y += this.peso
        this.app.drawLine(this.position, this.forcaNormal, pc.Color.GREEN, false, this.worldLayer)

    }
}

// update code called every frame
LineVectors.prototype.update = function (dt) {
    this.position = this.entity.getPosition()
    this.peso = (this.entity.rigidbody.mass * -gravidade) / 50 //Divided by 50 to get a better visual scale

    this.forcaPeso = this.entity.getPosition().clone()
    this.forcaPeso.y -= this.peso

    this.app.drawLine(this.position, this.forcaPeso, pc.Color.RED, false, this.worldLayer)
}

Some variables are in portuguese, but in short I’ve used the Math.sin and Math.cos to make the line normal (in physics) to make this line 90 degrees from the contact with the plane and the ramp

1 Like