georage Posted February 10, 2017 Share Posted February 10, 2017 Hey, I am not great with Babylonjs, but have managed to put together a few timesaving functions. I am sure you have too. If you have any that you thought were clever, please share them. None of mine are clever, but they are useful. Here is the humblest of all to get the ball rolling. All it does it output to the console for development. But once you go into production you can turn off all the console messages by commenting out one line instead of hundreds or thousands. function log(msg) { console.log( msg ); } I set and get global values with the following. There may be a better way but this is how I store player login information instead of using PHP sessions. function setGlobal(varName, varValue){ var global = $( "body" ).data( varName, varValue ); return global; } function getGlobal(varName){ var global = $( "body" ).data( varName ); return global; } function removeGlobal(varName){ $( "body" ).removeData( varName ); } Three more utility functions you may find helpful //converts from degrees to radians function degreesToRadians(degrees) { return degrees * Math.PI / 180; } //converts from radians to degrees function radiansToDegrees(radians) { return radians * 180 / Math.PI; } function deleteAllMeshes(scene){ console.log(scene.meshes.length + " meshes detected"); var countMesh = 0; for (var i = 0; i < scene.meshes.length; i++) { scene.meshes[i].dispose(); countMesh++; i--; } console.log(countMesh + " meshes deleted"); } Here's a way to format a date so it has leading zeroes with numbers under 10. Otherwise the width of the output will jump around. Remember to use a monospaced font. function formatDate(d){ function pad(n){return n<10 ? '0'+n : n;} function pad2(n){ if (n < 10) return '00'+n; else if (n < 100) return '0'+n; else return n; } return d.getFullYear()+'-'+ pad(d.getMonth()+1)+'-'+ pad(d.getDate())+' '+ pad(d.getHours())+':'+ pad(d.getMinutes())+':'+ pad(d.getSeconds())+'.'+ pad2(d.getMilliseconds()); } Calculate distance between meshes? function getDistanceBetweenMeshes( mesh1, mesh2 ) { //log("mesh1="+mesh1); //log("mesh2="+mesh2); var v1 = mesh1.position; var v2 = mesh2.position; var dx = v1.x - v2.x; var dy = v1.y - v2.y; var dz = v1.z - v2.z; var result = Math.sqrt( dx * dx + dy * dy + dz * dz ); return result.toFixed(5); } and the final entry for now ... it could be refactored to find the orbit distance of a planet based on the planet speed. function calculateOrbitalSpeed(starSize, planetSize, planetOrbit) { //the formula for obital speed is ... planet velocity = SQRT (gravity constant • mass of star / radius of orbit) //but since the planet mass is negligible we are just using the mass of the star var planetOrbitRadius = planetOrbit/2; //var gravityConstant = (starSize*planetSize)/Math.pow(planetOrbitRadius, 2); var gravityConstant = (starSize)/Math.pow(planetOrbitRadius, 2); planetSpeed = Math.sqrt(gravityConstant*starSize)/planetOrbitRadius; return planetSpeed; } GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 10, 2017 Share Posted February 10, 2017 Thank you for this, this will surely help a lot of beginners! georage 1 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.