Jump to content

Rotating a heightmap with physics


Recommended Posts

I'm sure there's probably a really simply explanation for this, and I've certainly put in the requisite time researching and experimenting... but I'm having a hard time getting collisions/physics to work properly with heightmaps.

Here's the playground: https://www.babylonjs-playground.com/#UV2S4R#1. 

Basically, the balls fall until a collision, then they slow down, which is working as expected. However, when you uncomment line #25 (`ground.rotation.y = .1;`), it's almost like the mesh rotates, but the physicsImpostor does not. It seems like I recall reading somewhere that you have to use a Quaternion to rotate meshes with impostors, but I can't find that reference for the life of me.

Any guidance would be much appreciated!

Link to comment
Share on other sites

Hi @fort,

There seems to be a problem with the heightmap impostor, and that is the reason why it doesn't work as you expect. As a workaround, until I fix this, you can use the mesh impostor (might hurt performance a bit thou).

Regarding rotation - if you set the rotation before (!) setting the impostor, you can use rotation with no problem. After setting the impostor, it is recommended to use the rotationQuaternion or the .rotate() function,

Link to comment
Share on other sites

Hi guys. 

Fort, by default, the Oimo physics engine is used.  Your line 18... doesn't specify CannonJS, and I believe Oimo has no heightMap physics capabilities.  (It does, but it "cloths" the ground to do it, and we don't have an automated method for that, yet.)   We should probably install a warning when folks try to use a heightMapImpostor... with Oimo, eh? 

I COULD be wrong about this info, but... there is no mention of 'heightmap' in https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/Plugins/babylon.oimoJSPlugin.ts.  There is mention of it... in https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/Plugins/babylon.cannonJSPlugin.ts.

So, here we go with CannonJS...


I have no solutions to actual issue, yet... but #3 is a CannonJS heightMap. The balls are rolling-off the mountains in a way that indicates that the impostor is matching the shape of the terrain contours.  (yay)  Further tests:


Here, same playground, except after 3 seconds, I set an angularVelocity on the ground impostor (lines 63-65).  After 10 seconds of watching... the balls definitely have a strange roll pattern, as if the impostor is indeed rotating, but the heightMap mesh is not coming-along.  hmm.  This would indicate that the meshShape and the impostor... are not "bound" to each other, somehow.  (ya think, wingy?)  heh

Ok, that's all I have so far.  :)

Link to comment
Share on other sites

@aWeirdo :lol:

@Wingnut thanks for the help, and great to know re: physics engine—was definitely under the impression that the playground used CannonJS by default. 

33 minutes ago, Wingnut said:

This would indicate that the meshShape and the impostor... are not "bound" to each other, somehow. 

This matches my observations as well.

Link to comment
Share on other sites

Should we go further, fort? 

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

That is within a function called _updatePhysicsBodyTransformation(), which happens with every physicsImpostor creation... for all types of impostors.

But... IF it's a heightMap... THEN BIG FAT chunk of work, eh? (lines 359-388 - gruesome!)

See line 374...  " //calculate the new center using a pivot (since Cannon.js doesn't center height maps) "

Yuh, yuh, yuh.  Since heightMaps are derived from textures, Cannon heightMap treats "origin" as 0,0 on the texture (lower left corner).  BJS heightmaps have their 0,0 origin in the center.

So... wow... it takes quite a bit of manipulation... to keep BJS heightMap aligned-with Cannon heightMap.  I think RaananW coded this section, and we need to make sure his authoring brain tumor has healed... before we ask him to re-examine this scary code.  The more troubleshooting/testing work that you/I/others get done FIRST (before Raanan), the less chance that Raanan returns-to the mental institute.  :D

Let's hope the solution is a simple observable binding... one line fix... and nobody gets a brain tumor.  But just in case... we should have medical first-responders standing-by.  heh

Link to comment
Share on other sites

Hmm... https://www.babylonjs-playground.com/#1RKZXB#80

Appears to use Oimo/default.  Perhaps I was wrong about Oimo heightMaps.  This seems to be one.  Sorry for earlier bad info, readers.

My angularVelocity setter after 3 secs seems to be working in some way... but again, the ground mesh never seems to rotate, even though its impostor has angularVelocity.  Still testing/learning.

Link to comment
Share on other sites

@aWeirdo, the earth is obviously flat. This is how it works - https://www.babylonjs-playground.com/#1BZJVJ#30

About Oimo vs Cannon - Oimo doesn't support heightmap, as it only supports basic types (sphere, cylinder, box). The default physics engine is CannonJS, especially due to its support of extra object types like trimesh and heightmap.

Link to comment
Share on other sites

Wanted to update regarding this issue - 

My initial thought was wrong - the heightmap is calculated correctly. The problem is the rotation pivot, which was not taken into account.

Cannon's heightmap is not centered (and requires a 90 deg rotation on the x axis), and those factors were not taken into account when setting the new rotation and translation of the physics object.

I have been experimenting for a long time already, trying to find the right solution, so far with no avail. As I know where and how to solve this, but simply missing something in the math, I am going to put it aside for a day or two and come back to it. A fresh point of view might not be the worst idea :)

Link to comment
Share on other sites

That's very kind, Fort.  You are an enjoyment to work-with, too... and you now have your "I Contributed to BJS Core" merit badge, because you found a bug.  Nicely done!

I'm honored to be allowed to hang-around-with the core coders for BJS.  I love looking at their code and hearing them talk.  It's how I learn.

Really, if there was a mesh.showImpostor() and impostor.showJoints()... our physics issues would be lowered by 75%.  But, we need all the physics speed we can get, and I fear that showing joints/impostors... would remove %50 of physics speeds.   Still, perhaps it would allow a slow-motion view of the physics actions of a scene.

There is a "thing" called geometry shaders.  WebGL doesn't have them yet, I hear.  But, perhaps, someday, shader code can be told to make basic geometry shapes, and those shapes... could be tracking physics impostor orientations.  Not sure.  I think I heard that geometry shaders can "create" basic geometry shapes, and our impostors are basic geometry shapes (except for heightmapImpostor and meshImpostor).  So, maybe... there is a better future.  :)

Here's the big question.  Will the physics engines be hardware-based in the near future?  Next gen computers?  Will that speed-up JS-marshalled physics ENOUGH... so that we don't need to worry about "bog"?  (I doubt it.  Hardware-based or not, I think physics speeds will ALWAYS be a concern for JS games.  We humans seem to want to add details to a scene until it starts to bog, no matter how many mesh and effects it takes.)  heh.

Thanks for the work, Raanan!  Thanks for the bug-find and kind words, Fort!  Thanks for BJS and its community, DK and friends!  Party on!

Link to comment
Share on other sites

  • 1 month later...

So! finally found what went wrong. The latest physics PR should solve this issue completely. Here is an example : https://playground.babylonjs.com/#UV2S4R#8

PS - the rotation must be applied AFTER the physics impostor was created. I was trying to find the reason why, but I think I spent a lot more time on this than I thought I would, and this is not a bad compromise. 

Link to comment
Share on other sites

Hi R, thx for the work.  Possible trouble, though.

http://playground.babylonjs.com/indexstable#RLKVFF#57  (x,y,z,shifted-x,y,x, and "L" keys active)

Although my landing gear "mess" (in BJS-stable-mode) acts differently each time it runs, it's 45-degree spring-loaded legs... are somewhat operational.

In "BJS-latest-mode", it looks as if the springs have failed.  

You didn't use those "lesser" off-brand spring-anchors for your recent fixes, did you?  ;)

I think BJS springs are too strong for those cheapo spring-anchors.  heh.

Link to comment
Share on other sites

Ok, R-man, thx for info.  No hurry on my issue... I am off-topic for Fort's thread.  I figged I'd tell you about my issue, just in case it is related to Fort's issue.  :)

We can work in a separate thread or Wingnut Chronicles... on http://playground.babylonjs.com/index.html#RLKVFF#60

(lots smaller - 2 joints, one spring, one hinge).  Sorry for the interruption, guys. 

Keep rockin' the heightMaps... that's a super-important physics area.  #1-ranked usage of physics engines, I suspect.  Thx!  (Wingy hugs a nearby heightMap

Space Taxi 3D!  Lunar Lander 3D!  Yay!  :)

Link to comment
Share on other sites

So! @Wingnut - the spring bug is fixed, should be in the playground soon.

@fort - after this PR is merged and on the playground (https://github.com/BabylonJS/Babylon.js/pull/3107) you can set the rotation prior to creating the impostor. 

Now I hope everyone is happy and I can get a good night sleep. Or can I?....

Link to comment
Share on other sites

Thx R!  Well done.

BJS labor is the price you pay for being a GOD!  :)

I hope you are kidding-around about no-sleep, R!  I don't think anyone should ever lose sleep because of a hobby.  Take it easy on yourself.

Only 5 lines changed?   Interesting.  (Wingnut tries to teach a Wingnut to fish, et al.)

Next time one of my gorgeous playgrounds breaks... (likely, a matter of hours), I'll be able to fix it myself beg yet again, for help.  heh.

But really... thx R!

Link to comment
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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...