rapier.js icon indicating copy to clipboard operation
rapier.js copied to clipboard

How to draw cuboid ?

Open myagoo opened this issue 3 years ago • 7 comments

I'm using rapier2d-compat version 0.7.6.

I'm creating a radial gravity based platform game.

I had no issue creating and rendering 2d ball object => I create a body, attach it to a Ball shaped collider, then in the game loop, I iterate over all collider and draw them according to their ShapeType. In case of ball ShapeType, I use the body translation property and the collider radius property.

Then I tried to create a simple square with cuboid() with the following code

const heroBodyDesc = RAPIER.RigidBodyDesc.newDynamic().setTranslation(
  100,
  100
);
const heroColliderDesc = RAPIER.ColliderDesc.cuboid(10, 10).setDensity(
  1
);
let heroBody = world.createRigidBody(heroBodyDesc);
let heroCollider = world.createCollider(
  heroColliderDesc,
  heroBody.handle
);
console.log(heroCollider.vertices(), heroBody.mass());
// logs : undefined 400

I do not understand how to retrieve the shape infos, there is no width or height nor vertices on the collider. Could someone give me a hand ?

myagoo avatar Dec 27 '21 14:12 myagoo

I'm sorry I've just discovered that it is not the good repo to post this

myagoo avatar Dec 27 '21 14:12 myagoo

@myagoo I transferred the issue to the rapier.js repo.

You can retrieve the cuboid’s half extents with heroCollider.halfExtents().

sebcrozet avatar Dec 27 '21 15:12 sebcrozet

Thank you for the quick response, but I now face the same issue with a triangle shape

            let collider = world.createCollider(
              RAPIER.ColliderDesc.triangle(
                new RAPIER.Vector2(heroPosition.x + 10, heroPosition.y + 10),
                new RAPIER.Vector2(heroPosition.x, heroPosition.y + 10),
                new RAPIER.Vector2(heroPosition.x, heroPosition.y)
              )
                .setDensity(1)
                .setFriction(1)
                .setRestitution(1),
              rigidBody.handle
            );
            console.log(collider.indices(),collider.vertices(), collider.halfHeight(), collider.halfExtents())
            // undefined undefined undefined null
           

myagoo avatar Dec 27 '21 20:12 myagoo

Also, Is it mandatory to create new RAPIER.Vector2 where it is expected or can I just use a plain vector object ?

myagoo avatar Dec 27 '21 20:12 myagoo

It looks like there is no method to retrieve the triangle’s vertices yet. By the way, you can see on the documentation of the collider’s methods like .halfExtents, .vertices, etc. what method returns a non-undefined value for what shapes.

Also, Is it mandatory to create new RAPIER.Vector2 where it is expected or can I just use a plain vector object ?

Any object with accessible x and y fields should work.

sebcrozet avatar Dec 27 '21 21:12 sebcrozet

While waiting for a way to get informations for triangle shape directly from the collider, how can I use the convex polygon to create a triangle ? I don't understand how to use the vertices: Float32Array argument

(Thank you for thoses quick answers anyway)

myagoo avatar Dec 27 '21 22:12 myagoo

While waiting for a way to get informations for triangle shape directly from the collider, how can I use the convex polygon to create a triangle ? I don't understand how to use the vertices: Float32Array argument

(Thank you for thoses quick answers anyway)

   g.beginPath();
    for (let i = 0, j = vertices.length; i < j; i += 2) {
        const v = this.multiplyM2P(vertices[i], vertices[i + 1]);   // meter to pixel

        if (i == 0)
            g.moveTo(v.x, v.y);
        else
            g.lineTo(v.x, v.y);
    }
    g.closePath();
    g.strokePath();

jcyuan avatar Jan 08 '22 12:01 jcyuan