PSLS.net Home

Daily Reaction Game Dev: Stuck is Stuck…Except When it’s Rotating

April 30, 2013 Written by Dan Oravasaari

Makeitstop

Another week, another update on the best game programmers who have ever co-hosted a daily feature called Daily Reaction on PlayStation LifeStyle named Seb and Dan. This week we get stuck spinning in circles without the ability to stop, we learn that sometimes things don’t go the way you want and that we should have paid more attention in math class.

Dan: Starting out trying to learn to write a game we understood that we would have a few issues with learning to write certain sections, but neither of us ever thought that we would get held up on one of the most simple concepts, rotation.

Seb: Oh god, I even hate the word rotation now. But let’s not get ahead of ourselves – what we were trying to do is make our cube rotate clockwise when it moved down and anticlockwise when it moved up. But here’s the tricky bit – we only wanted it to rotate a little, not keep rotating round and round (think like a ship on the waves).

Dan: Getting an object to rotate on an axis was fairly simple, as you can simply use the transform function to call ‘Rotate” to get an object to rotate along a set axis, but understanding the concept and the difference between ‘transform.Rotate’ and ‘transform.rotation’ had stumped us from the outset.

transform.Rotate(5,y,z);

var degreeOfRotation : float = transform.rotation.x;

The difference became simple as we understood, Rotate is the class expecting a value to actually cause the GameObject to rotate, while rotation is returning a value of the current float of the object’s rotation.

Seb: Whichever depraved mind created this code should be locked up. But yes, we worked out that’s what we had to do to make the blasted thing rotate. Now, how do we stop it rotating when we want to? We thought that using Mathf.Clamp would be the answer, that it would set the limits to which angles the cube could rotate on. This is what we came up with:

 if( Mathf.Abs ( moveVert ) > 0.0 ) //moveVert +1 when space bar is pressed

 {

     transform.position += Vector3.down * Time.deltaTime * 3;  //move down

     transform.Rotate(0,0,(Mathf.Clamp(transform.rotation.z,335,360)));

    //not working Mathf.Clamp

 }

 It didn’t work.

Dan: Mathf.Clamp is a Class that is supposed to keep the range of values for a variable within a set range, but when being used for rotation a number of issues popped up. The first hurdle was simply understanding that the value for rotation is not a simple 0 through whatever degree, it is a circle with values that loop around, resetting back to 0 after reaching 360. Meaning to clamp a value between a 45 degree angle up and down, starting with a base of 0 degrees forces the clamp to max the transform.rotation.x at 45, and 315 on the clockwise rotation. But, the issue was that the Clamp was not understanding the returned value of rotation, so it would not stop the rotation from going up, or down.

 Seb: At this stage, we just wanted to work out what was wrong. So we looked, we searched, we tried things and we pulled out hair out (well I did, Dan’s bald). I tried transform.rotation.eulerAngles.z, but didn’t have any luck.

Meanwhile, Dan tried to have the ‘game’ use notifications to show if the rotation was even being measured.

Dan: The issue we learned was that we were having a number of variables interacting, but did not know if they were actually having the appropriate values being sent or returning, so we set out a number of prompts to show us how the system was handing the values we set.

Debug.Log(“This shit broke”);

Seb: And, as you’d expect, it did not react how we expected. In short, so far, we were stumped.

But do not despair, PSLS faithfuls. We have an idea on how to solve this tricky dicky conundrum. We also managed to make headway in other fields, such as making our cube look like our prototype character, as well as experimentations on creating a sine-wave-like motion. Hopefully we can pull this all together in time for the next DR Game Dev, because, in that case, we’ll be significantly further along the path to our ultimate goal of releasing a game.

Are you spinning in your chair reading this? Do you like math functions? Was your dad named Euler? Let us know in the comments why we have no idea what we are doing, or ideas you may have on what went wrong, or by tweeting us pics of animals to keep our spirits up at Seb and Dan. Also, feel free to shoot us an email at DailyReaction@PlayStationLifeStyle.net for a chance to have your email read on the next episode of Bad Gamers the next time Seb is in that rose petal laden bathtub.