Defcronyke

How to make a sphere roll down a triangular prism

Recommended Posts

I am new to 3D game dev, and I'm wondering how to make a triangular prism that a sphere could roll down. I think I'm missing something huge, because physics libraries only seem to support the most basic geometries, excluding triangular prisms and other important shapes. Could someone please point me to some resources or explain how to deal with physical interactions between more complicated shapes? I notice that BabylonJS makes it easy to create lots of neat meshes, however, I'm missing how to simulate their shapes on the CannonJS side of things. I want to make a game where the player controls a sphere by rolling it around various scenes, but it just doesn't seem possible using only spheres and boxes, for example.

Share this post


Link to post
Share on other sites

Hiya Defcronyke, welcome to the forum... good to have you with us.

Use the CannonJS physics engine and use the meshImpostor.

Unfortunately, meshImpostor ONLY interacts with spheres, but that is exactly what you are using, so, perfect match. 

For rolling/moving the sphere, use applyImpulse or setLinearVelocity.  Remember that spheres have no significant friction, so, it is difficult to STOP a rolling sphere.  Many times, it is best to setLinearVelocity (AND angularVelocity) = new BABYLON.Vector3(0, 0, 0)... to stop a moving sphere.

Also keep in mind that physics-active mesh... use mesh.rotationQuaternion instead of .rotation.

Good luck, keep us posted.

Share this post


Link to post
Share on other sites

Thanks Wingnut, MeshImpostor is almost perfect, the sphere collides with the prism in the correct ways, except I didn't want the prism to have mass: 0, but if I give it a mass, it falls through the floor. The floor is a BoxImpostor...

Share this post


Link to post
Share on other sites

Ah yes.  :)  Sorry about that.  I wonder if meshImpostors react with meshImpostors.  I doubt it.  But heck, try making the floor a meshImpostor, too.  It probably won't work, but, what the heck... it's worth a try.

The other option might be... make the ground be a sphere... very down-scaled Y.  (a very flat sphere, like a big pancake)... and then use a sphereImpostor on IT, too.  Kind of weird, but better than peeing on your foot, right?  :D  Or perhaps put that flat pancake (with sphereImpostor) just BARELY underneath the standard ground.

In fact, would a SMALL sphere (or 4 of them) with no mass... placed under the pyramid... work?  It/They might be hidden by the pyramid that is atop it/them.

I'll keep thinking.  :)  Good to hear that the meshImpostor is working.

Share this post


Link to post
Share on other sites

MeshImpostors don't collide with each other, and I can't figure out how to flatten the SphereImpostor. I can visually flatten the mesh with mesh.scaling.y = 0.1, but the physics body doesn't get scaled with it. I tried setScalingUpdated(true) and forceUpdate(), but the scaling on the physics body still remains as its original shape.

Share this post


Link to post
Share on other sites

When you create a Cannon sphere shape, the function takes a single value, the radius. This value is then used to create the sphere. You won't be able to, as far as I know, assign different values for width, depth and height. At least not without changing the Cannon library itself.

Share this post


Link to post
Share on other sites

Unfortunately, I'm not experienced with the impostor-system, but I know it is indeed possible with native Cannon, so I'd assume it is possible using the impostor-system as well.

Look at this example: https://schteppe.github.io/cannon.js/demos/shapes.html

How is your prism supposed to look like?

The shape closest to the upper left cornor is a ConvexPolyhedron, Same thing, I figure, as the meshImpostor. Try turning the scene upside-down. Is this the shape you want? Or are you thinking of a more traditional triangle, you know, like the roof of a very basic house? If it's the latter, you might as well make a cylinder shape. In the scene you see 2 cylinders. Both have 10 segments. If you create a Cannon shape with only 3 segments, that will make a 3-dimensional triangle. 3 is the lowest amount of segments a cylinder is allowed to have in Cannon.

 

Share this post


Link to post
Share on other sites

The traditional triangle is what I want, as created with BABYLON.MeshBuilder.CreateCylinder('triangularPrism1', {tessellation: 3}, this.scene), it looks right, however when I attach a CylinderImpostor to it, the prism rolls like a cylinder, which is not what I want.

Share this post


Link to post
Share on other sites

Sorry about the bad info about flattening sphereImpostors.

------

I think you can use the meshImpostor... on your prism, even though it is built with a cylinder.  Should be no problem.

Um... let's see.

1. Set ground with boxImpostor and 0 mass.  Good solid ground.  :)

2. Um... TRY parenting 3 tiny boxes... with boxImpostors, onto the bottom of the prism 'legs'.  Try to make them VERY small... but they might fall thru floor if TOO small.  You may need to use 7 of them  :)  As always, children get physicsImpostors BEFORE parents... in the code sequence.

    2b.  If parenting/positioning is successful, drop the prism with its new tiny boxImpostor-feet... onto the ground.  It SHOULD remain above ground, and could even "skid" a little... like it's on ice.  (cool)

    2c.  If parenting/positioning fails, perhaps de-parent 3-7 boxImpostor-feet, and instead, attach a physics joint between meshImpostor prism... and all "prism feet".  We are giving our prism's feet... some knees.  :)  Joints are the "physics way" to do parenting.  Joints can be set to have ALMOST zero-flexibilty (bone-like).

3.  Test by dropping prism onto ground.  We are attempting to make the GROUND keep the prism feet elevated, and make the PRISM FEET keep the prism elevated.  A little cooperation. 

4.  Do that exact same thing, but use a SINGLE PLANE parented-onto or jointed-onto the bottom of the prism, and give it a planeImpostor.  That sounds easier, and the planeImpostor should stay atop the boxImpostor ground.  Yeah!  That's the ticket!  Try this way FIRST. :)

* Keep in mind that mesh with .visibility = 0... can STILL have impostors (possibly wonderful for you).

Share this post


Link to post
Share on other sites
8 hours ago, Defcronyke said:

The traditional triangle is what I want, as created with BABYLON.MeshBuilder.CreateCylinder('triangularPrism1', {tessellation: 3}, this.scene), it looks right, however when I attach a CylinderImpostor to it, the prism rolls like a cylinder, which is not what I want.

That is because the cylinderImpostor is automatically created using 16 segments, instead of taking into account the tesellation of the mesh itself.

https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/Plugins/babylon.cannonJSPlugin.ts#L239

I was mistaken. The meshImpostor is a trimesh, which, if nothing has changed,, only collides with spheres and planes.

You might have to go native on this one. Natively, you could specify the shape of the Trimesh:

http://schteppe.github.io/cannon.js/docs/classes/Trimesh.html

Or a ConvexPolyhedron:

http://schteppe.github.io/cannon.js/docs/classes/ConvexPolyhedron.html

Or, as already mentioned, create a cylinder with 3 segments:

http://schteppe.github.io/cannon.js/docs/classes/Cylinder.html

The radiusTop and radiusBottom will define the width and depth, and the height will define the length of the triangle.

I think this would be the most performant way of doing it.

 

EDIT: This isn't quite right, but it's a starting point: http://www.babylonjs-playground.com/#LEWTG2#1

:P

 

Share this post


Link to post
Share on other sites

I attached spheres as children to the 5 sided prism, positioned just inside the prism mesh, giving it feet, and it worked, however it was very hard to line up the spheres, because the axis were not the same as in world-space, and it really confused me. I think until MeshImpostor can collide with other shapes than just sphere and plane, I will have to just use more basic shapes, because it takes too long and too much code to attach all sorts of sub-shapes to everything. It's really messy. I don't really understand why a HeightmapImpostor which I have working, can collide with other shapes, but a MeshImpostor can't. It seems like a silly limitation.

Thanks for the help Wingnut and Raggar. Hopefully in future versions of BabylonJS, they make the MeshImpostor able to collide with other shapes.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.