Jump to content

Basic Math Knowledge for Video Games


ProfessorF
 Share

Recommended Posts

I'm writing a research article on infographics, and one of the examples I created was an infographic for videogames:

 

post-5799-0-73415700-1388687428.png

 

I was reminded of this graphic as I was programming a third-person-follow camera in Babylon.js.  You can use these basic math concepts to move characters, move missiles, to position cameras etc.  And this is even the basic math you need to know for "monsters" orienting towards a player and chasing the player.

 

While this looks like it's just for 2D games, you can use it for basic 3D games--just think in terms of the XZ instead of the XY coordinate system.

 

Anyway, yes, I use a lot of linear algebra knowledge for more advanced graphics programming, but for basic "stuff" the knowledge in this diagram is good enough (I think).

 

Feedback would be appreciated. I'd like to hear what other basic math the community uses.

 

Thank you in advance,,

 

- Nick 

 

Update: Below is a more general infographic, which may be of use to highschool math teachers for trignometry & geometry problems.(I almost never use the arcsin, arccos  functions in my virtual worlds--dx, dy, arctan, distance formula) 
 

post-5799-0-68793900-1388761614.png

Link to comment
Share on other sites

Good point reddozen.  I like learning about all the "tweaks" developers make to the math.

 

Dad72, depending on the game, you may not need this math at all.  But if you have to rotate your avatar (A) towards some object (O), you need an angle, and that's the math I use to figure out that angle.  Although be careful in Babylon.js because 3D characters are already rotated -90 degrees (like dude).  So if you want dude facing 45 degrees, you have to actually rotate -(90+45).  

I'll add some examples to this thread.  But in one of the posts I show the code for clicking on a plane and orienting + moving Dude towards the click.  I'll clean it up and repost here using the variables in the infographic.

 

Thanks guys!

Link to comment
Share on other sites

Dad72, here's one way I use the math:

 



Below is the exact code I used.  

 

Note:

1) All y-variables are replaced by z-variables because the plane is XZ, not XY

2) Where I click becomes ox,oz

3) Dude starts off rotated 90 degrees so you, can't just do: A=atan2(dz, dx). You have to correct: A=-(Math.PI/2+atan2(dz, dx))

 

CODE using the basic math. If you use this code, you will have to put dude.babylon in the folder Assets/Dude:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>Tutorial: Basic Math for Video Games</title>    <script src="js/babylon.js"></script>    <script src="js/hand.js"></script>    <script>        window.addEventListener("load", start);  // first line of code executed        window.addEventListener("click", handleClick);         var canvas, engine, scene, light, camera, zero, ground,            Avatar, ox, oz, dx, dz, A, D;        function start() {            canvas = document.getElementById("cvRAVE");            engine = new BABYLON.Engine(canvas, true);            scene = new BABYLON.Scene(engine);            light = new BABYLON.DirectionalLight("sun", new BABYLON.Vector3(1, -1, 1), scene);             camera = new BABYLON.FreeCamera("cam", new BABYLON.Vector3(0, 8, -10), scene);            ground = new BABYLON.Mesh.CreateGround("ground", 1024, 1024, 1, scene);            ground.material = new BABYLON.StandardMaterial("texGround", scene);            ground.material.diffuseTexture = new BABYLON.Texture("geotrigtexture.png", scene);            BABYLON.SceneLoader.ImportMesh("him", "assets/dude/", "dude.babylon", scene, function (meshes, particles, skeletons) {                Avatar = meshes;                Avatar[0].scaling = new BABYLON.Vector3(.1, .1, .1);                scene.beginAnimation(skeletons[0], 0, 120, true, 1.0);            });            scene.activeCamera.attachControl(canvas);            engine.runRenderLoop(update);        }        function update() {            if (Avatar) {                ax = Avatar[0].position.x;                az = Avatar[0].position.z;                dx = ox - ax;                dz = oz - az;                D = Math.sqrt(dx * dx + dz * dz);                // Dude is rotated 90; Positive Y rotations are clockwise                // You need to correct dude by duderot+90--backwards (negative)                A = -(Math.atan2(dz, dx) + Math.PI / 2);                                if (D > 1) {                    Avatar[0].rotation.y = A;                    Avatar[0].position.x += (dx / D);                    Avatar[0].position.z += (dz / D);                                   }            }            scene.render();        }        function handleClick(event) {            // https://github.com/BabylonJS/Babylon.js/wiki/11---Picking-collisions            var picked = scene.pick(event.clientX, event.clientY);            if (picked.hit) {                ox = picked.pickedPoint.x;                oz = picked.pickedPoint.z;            }        }    </script>    <style>        html, body {            width: 100%;            height: 100%;            padding: 0;            margin: 0;        }        #cvRAVE {            width: 100%;            height: 100%;        }    </style></head><body>    <canvas id="cvRAVE"></canvas></body></html> 
Link to comment
Share on other sites

Thanks You ProfessorF

 

The ideal would be to have in Babylon in BABYLON.Mesh the function :

 

LookAt(worldPosition:Vector3, worldUp:Vector3 = Vector3.up);

Ennemie.position.LookAt(new BABYLON.Vector3(Player.position.x, Player.position.y, Player.position.z), BABYLON.Vector3.up);

Thus the enemy would look at in the direction of the player and move in his direction.

@DeltaKosh: It would be possible to have this feature in Babylon.

Link to comment
Share on other sites

With the function LookAt( ), it would be interesting also to be able to retrieve the distance between the player and the target.
For example: (Member "magnitude")

dirToMain = Player.position - Enemi.position;if(dirToMain.magnitude > 2) //play movement...else //stop movement

his would in addition to watch the target with "LookAt( )", to know the distance that the separated.

It would be also interesting to have a Member "velocity" for the movement of a mesh. (rigidbody.velocity) or/and function TransformDirection(BABYLON.Vector3.forward * speed)

 

What you think of that?

Link to comment
Share on other sites

the distance is simple to evaluate: Player.position.subtract(Enemi.position).length()

 

dad72, you may want to use getAbsolutePosition instead of position just in case Player and Enemi do not have the same parent. If they have the same parent or no parent, you are fine to use position like suggesting by deltakosh.

Link to comment
Share on other sites

That seems nice ProfessorF. Do you have to call LookAt any time you need it (in an update function for example) or is it like a flag that you add to the mesh so it will always look at its target? Something similar to physics constraint.

Yes, currently you have to call it in update. Although I can add a flag.  Thanks.

Link to comment
Share on other sites

Great. It could be nice to add a constraints mechanism to mesh like an array of callbacks which would be called at each update. Thus, your "flag" would be a callback to constrain the mesh to look at. I can think about another constrain "catch me if you can" ;) This constrain would be used instead of infiniteDistance for example.

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.

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...
 Share

  • Recently Browsing   0 members

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