moving algorithm

Started by
3 comments, last by Tera_Dragon 20 years ago
I've got this algorithm off nehe. I know what is does, but don't have a clue about how it does it if you can will you please explain every line of the code?

const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;

GLfloat walkbias = 0;
GLfloat walkbiasangle = 0;
GLfloat lookupdown = 0.0f;
GLfloat	z=0.0f;	



if (keys[VK_UP])
{

		xpos -= (float)sin(heading*piover180) * 0.05f;
		zpos -= (float)cos(heading*piover180) * 0.05f;
		if (walkbiasangle >= 359.0f)
		{
			walkbiasangle = 0.0f;
		}
		else
		{
			walkbiasangle+= 10;
		}
		walkbias = (float)sin(walkbiasangle * piover180)/20.0f;
}
I don't even know what the piover180 is as I said if you could give me a quite detailed explanation of each bit of code I would greatly apriciate it. If you need the rest of the code you can download it fromhttp://nehe.gamedev.net/data/lessons/vc/lesson10.zip (sorry I don't know how to make a link ) thanks in advance Tera_Dragon [edited by - Tera_Dragon on March 18, 2004 5:10:35 PM] [edited by - Tera_Dragon on March 18, 2004 5:11:18 PM]
____________________________________________________________Programmers Resource Central
Advertisement
Multiplying by pi/180 (0.0174582925) is how you convert from degrees (your heading is a degree value) to radians (sin and cos work with radians, not degrees).

So, it looks like what this code is doing is:
Check to see if the Up key is pressed
if (keys[VK_UP]){  

If so, take the current heading (whichever direction the character or view is facing, in degrees), and move the current position (xpos, zpos) in that direction by 0.05 units. If heading is an angular value in radians, then (sin(heading), cos(heading)) gives you the components for a unit-length vector pointing in that direction. Multiplying these values by 0.05 gives you the values you need to add to your current position to get the new position moving in that direction.
		xpos -= (float)sin(heading*piover180) * 0.05f;		zpos -= (float)cos(heading*piover180) * 0.05f;  


Without seeing more code, I'm not entirely certain what role walkbias and walkbiasangle play. Here, we are checking to see if walkbiasangle wraps around; that is, if it increases over 359 degrees, it wraps back around to 0. Otherwise, we are incrementing it by 10 degrees.
		if (walkbiasangle >= 359.0f)		{			walkbiasangle = 0.0f;		}		else		{			walkbiasangle+= 10;		}  


And, again, I am not sure what role walkbias plays, but here you are converting walkbiasangle from degrees to radians, taking the sin, and dividing the sin by 20. The purpose of this is hidden in other code not presented here.
		walkbias = (float)sin(walkbiasangle * piover180)/20.0f;}  


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

[edited by - VertexNormal on March 18, 2004 5:27:16 PM]
the walk bias was to give the effect of "bobbing" as one walks, so the camera bobbed up and down (I guess this was made he z-coordinate) and so when just before drawing trianlges or such probably there is a code that does something like

glTranslate(xpos,ypos,walkbias)
(or -xpos,-ypos depending on how the camera stuff is organized)

thus translating the vertices up or down by walkbias.
Close this Gamedev account, I have outgrown Gamedev.
thats correct

I want to know why he does this though:
if (keys[VK_RIGHT]){	heading -= 1.0f;	yrot = heading;}if (keys[VK_LEFT]){	heading += 1.0f;		yrot = heading;}

as you can see when the right or left arrow are pressed yrot is increased or decreased. I though that this would provdie the backward way the world turns (as the camera doesn''t really move), but when the yrot is used to rotate the screen it is done like this:
GLfloat sceneroty = 360.0f - yrot;


I want to know 2 things about this; why is 360 even used, as it does the same without it. and why do we have to - yrot and not plus it as we have already taken away from it?
Thanks for helping a n00b
Tera_Dragon
____________________________________________________________Programmers Resource Central
nobody?
____________________________________________________________Programmers Resource Central

This topic is closed to new replies.

Advertisement