Skip to content

Instantly share code, notes, and snippets.

@ajweeks
Last active January 31, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajweeks/bc1f586a340a6ea3b444 to your computer and use it in GitHub Desktop.
Save ajweeks/bc1f586a340a6ea3b444 to your computer and use it in GitHub Desktop.
Twenty-Ninth of Devember

Devember Day 29

I looked up how to flip meshes in three.js the other day when I went to the nearest library here in Vienna for their internet, and found a few stackoverflow.com answers which explained a few ways of doing what I wanted. So I implemented that today, and it's in the game now! It looks sweet, and somewhat surprisingly doesn't seem to cause any lag at all, even though it's updated every frame. The way I'm doing it by the way, in case you are wondering is:

if (xv < 0) {
    this.playerMesh.rotation.y = Math.PI;
} else if (xv > 0) {
    this.playerMesh.rotation.y = 0;
}

All that does is flip the plane that the player is rendered on around 180 degrees (pi radians). Since I set the player's material to be two sided, it simply renders the mirror image. I also set the tool mesh to be a child of the player mesh, so that when the player is flipped, so is the tool. I didn't really think about it much, but I was kind of surprised (and pleased) to see that the rotation code worked as is. That is because the player plane is literally just flipped around, and so the rotation is mirrored as well.

I then added a slight realism improvement. Before, the player could walk at a constant velocity upwards, and to the side, meaning they were covering a larger displacement by moving diagonally. To combat that, if they are walking diagonally, I divide their x and y velocities by the square root of 2. The reason for that is if the two sides opposite the hypotenuse in a right triangle are of length 1, then the hypotenuse has a length of the square root of two. I don't know how many games use something like that, although if they represent velocities as a magnitude and direction instead, then I think the end result would be the same. If I ever want to add a bit more complexity to the movement system in the future (like adding acceleration and friction) then I will probably use a vector to represent the velocity rather than an x and y value.

I'll be back tomorrow!

Previous Entry | All Entries | Next Entry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment