MackeyK24 Posted August 21, 2017 Share Posted August 21, 2017 In unity they have transform.forward, right and up. The docs for unity say the forward is : 'The blue axis of the transform in world space.' using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float thrust; public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { rb.AddForce(transform.forward * thrust); } } How would I do this in Babylon.... Yo @Deltakosh or ANYBODY How do we get the forward vector in Babylon... Would this work: /** The blue axis of the transform in world space. */ public static GetForwardVector(mesh:AbstractMesh, force?:boolean):BABYLON.Vector3 { var matrix:BABYLON.Matrix = mesh.computeWorldMatrix(force); return BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Forward(), matrix); } /** The red axis of the transform in world space. */ public static GetRightVector(mesh:AbstractMesh, force?:boolean):BABYLON.Vector3 { var matrix:BABYLON.Matrix = mesh.computeWorldMatrix(force); return BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Right(), matrix); } /** The green axis of the transform in world space. */ public static GetUpVector(mesh:AbstractMesh, force?:boolean):BABYLON.Vector3 { var matrix:BABYLON.Matrix = mesh.computeWorldMatrix(force); return BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), matrix); } Also I saw some c++ code for getting forward vector using the quaternion, could I use this in babylon or SHOULD I even try use in Babylon ... Is there something already in Babylon for this: Vector3 Quaternion::GetForwardVector() const { return Vector3( 2 * (x * z + w * y), 2 * (y * x - w * x), 1 - 2 * (x * x + y * y)); } Vector3 Quaternion::GetUpVector() const { return Vector3( 2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)); } Vector3 Quaternion::GetRightVector() const { return Vector3( 1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)); } Or am I way off Quote Link to comment Share on other sites More sharing options...
adam Posted August 21, 2017 Share Posted August 21, 2017 https://www.babylonjs-playground.com/#2609XG https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.abstractMesh.ts#L1832 GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
SvenFrankson Posted August 21, 2017 Share Posted August 21, 2017 If you want your first attempt to work, I think you need to use TransformNormal, and not TransformCoordinates. (because it's a direction / normal, not a position / coordinates) I don't know enough about Quaternion for the 2nd one, but if it works I guess it would be a pretty optimized way to do it. Only issue would be that unlike in Unity, rotation and rotationQuaternion are not binded together. Quote Link to comment Share on other sites More sharing options...
MackeyK24 Posted August 23, 2017 Author Share Posted August 23, 2017 I think I got it... These are my little "Unity-Like" Helper functions on the SceneManager class: // *********************************** // // * Update Actor Helper Support * // // *********************************** // /** Applies force to actor using physics imposter. */ public addForce(owner:BABYLON.AbstractMesh, force:BABYLON.Vector3, contact:BABYLON.Vector3) : void { if (owner != null) { if (owner.physicsImpostor != null) { if (force != null && contact != null) owner.physicsImpostor.applyForce(force, contact); } else { BABYLON.Tools.Warn("Physics imposter not defined for mesh: " + owner.name); } } } /** Applies impulse to actor using physics imposter. */ public addImpulse(owner:BABYLON.AbstractMesh, force:BABYLON.Vector3, contact:BABYLON.Vector3) : void { if (owner != null) { if (owner.physicsImpostor != null) { if (force != null && contact != null) owner.physicsImpostor.applyImpulse(force, contact); } else { BABYLON.Tools.Warn("Physics imposter not defined for mesh: " + owner.name); } } } /** Moves actor using physics imposter. */ public moveWithPhysics(owner:BABYLON.AbstractMesh, velocity:BABYLON.Vector3, angular:BABYLON.Vector3 = null) : void { if (owner != null) { if (owner.physicsImpostor != null) { if (velocity != null) owner.physicsImpostor.setLinearVelocity(velocity); if (angular != null) owner.physicsImpostor.setAngularVelocity(angular); } else { BABYLON.Tools.Warn("Physics imposter not defined for mesh: " + owner.name); } } } /** Moves actor using camera collisions. */ public moveWithCollisions(owner:BABYLON.AbstractMesh, velocity:BABYLON.Vector3) : void { if (owner != null && velocity != null) { owner.moveWithCollisions(velocity); } } // ************************************ // // * Public Transform Tools Support * // // ************************************ // /** Transforms position from local space to world space. */ public transformPosition(owner: BABYLON.AbstractMesh | BABYLON.Camera, position:BABYLON.Vector3):BABYLON.Vector3 { return BABYLON.Vector3.TransformCoordinates(position, owner.getWorldMatrix()); } /** Transforms direction from local space to world space. */ public transformDirection(owner: BABYLON.AbstractMesh | BABYLON.Camera, direction:BABYLON.Vector3):BABYLON.Vector3 { return BABYLON.Vector3.TransformNormal(direction, owner.getWorldMatrix()); } // ************************************ // // * Scene Direction Helper Support * // // ************************************ // /** Gets the blue axis of the owner in world space. */ public getForwardVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 { return owner.getDirection(BABYLON.Vector3.Forward()) } /** Gets the red axis of the owner in world space. */ public getRightVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 { return owner.getDirection(BABYLON.Vector3.Right()); } /** Gets the green axis of the owner in world space. */ public getUpVector(owner: BABYLON.AbstractMesh | BABYLON.Camera):BABYLON.Vector3 { return owner.getDirection(BABYLON.Vector3.Up()); } // ************************************* // // * Public Quaternion Tools Support * // // ************************************* // /** Computes the Quaternion forward vector */ public getQuaternionForwardVector(quat:Quaternion):BABYLON.Vector3 { return new BABYLON.Vector3( 2 * (quat.x * quat.z + quat.w * quat.y), 2 * (quat.y * quat.x - quat.w * quat.x), 1 - 2 * (quat.x * quat.x + quat.y * quat.y)); } /** Computes the Quaternion right vector */ public getQuaternionRightVector(quat:Quaternion):BABYLON.Vector3 { return new BABYLON.Vector3( 1 - 2 * (quat.y * quat.y + quat.z * quat.z), 2 * (quat.x * quat.y + quat.w * quat.z), 2 * (quat.x * quat.z - quat.w * quat.y)); } /** Computes the Quaternion up vector */ public getQuaternionUpVector(quat:Quaternion): BABYLON.Vector3 { return new BABYLON.Vector3( 2 * (quat.x * quat.y - quat.w * quat.z), 1 - 2 * (quat.x * quat.x + quat.z * quat.z), 2 * (quat.y * quat.z + quat.w * quat.x)); } Working great so far GameMonetize and SvenFrankson 2 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.