Bombadilo101 Posted December 25, 2015 Share Posted December 25, 2015 Is there some built in way to find the velocity of an object? Was hoping to build in a maximum velocity and variable impulse for my spaceship game but was having trouble finding out whether or not an object was moving in space. Currently using cannon.js for the physics engine. Apparently if you use cannon with Three.js you can just do object.velocity.y or object.velocity.norm() but it doesn't seem to work in babylon. Quote Link to comment Share on other sites More sharing options...
RaananW Posted December 27, 2015 Share Posted December 27, 2015 When creating a physics object you get its body as the return value. You could use this body just like in three.js. You could also get the physics body of a mesh using the physics engine object of your scene. Quote Link to comment Share on other sites More sharing options...
Bombadilo101 Posted December 27, 2015 Author Share Posted December 27, 2015 strange... seems there's no velocity check after all. Not sure if they took it out or renamed it, but I ended up going with this: var zCheck = truevar veloZ = PC.absolutePosition.z; scene.registerBeforeRender(function(){ if (Math.abs(PC.absolutePosition.z - veloZ) > 10) { zCheck = false } else { zCheck = true } veloZ = PC.absolutePosition.z}); not pretty, but it seems to work. I feel like there's got to be an easier way though. What's a physics engine without a built in velocity checker. Also I'm not certain whether renders are over a variable length of time or not so my checker could be seriously flawed. just a z-axis check for the moment, still deciding whether to go with a 2-d or 3-d speedcheck in order to limit max impulse. Quote Link to comment Share on other sites More sharing options...
Wingnut Posted December 28, 2015 Share Posted December 28, 2015 Hi Bombadilo101, welcome to the forum. http://www.babylonjs-playground.com/#1NMU7M Watch your console. Built-in velocity... in CannonJS. Yay! Like Raanan said, when you setPhysicsState on a mesh, a physics "body" is returned to you. (line 27 - "sbody") In line 30, I am printing the Y-axis velocity of sbody.... to console. Here is the CannonJS API... for 'body'... http://schteppe.github.io/cannon.js/docs/classes/Body.html As for CannonJS unpredictable behavior, I can't help you there. I prefer using OimoJS, actually. Below is the same thing in Oimo, but notice there is a change in line 30. Now it's sbody.body.linearVelocity. And, I did some minor adjustments to the restitution (bounce) values. And of course, line 8, switched to the Oimo plugin. Notice the bounce is more predictable in OimoJS. http://www.babylonjs-playground.com/#1NMU7M#1 OimoJS does not have a published API, but see http://urbanproductions.com/wingy/babylon/misc/j2h02.htm - search for com.element.oimo.physics.dynamics and then look for RigidBody nearby... property #12. Go get em, Bombadilo! You got the tools to get good and confused now. RaananW 1 Quote Link to comment Share on other sites More sharing options...
nittrus Posted January 25, 2020 Share Posted January 25, 2020 I know this is rather old but I feel this should be updated so others searching for a solution have up to date information so here ya go. mesh.getPhysicsImpostor().getLinearVelocity(); To get the velocity of rotation of an object use getAngularVelocity. mesh.getPhysicsImpostor().getAngularVelocity(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.