banking, and side stepping

This is a discussion on banking, and side stepping within the Game Programming forums, part of the General Programming Boards category; I'm curious on how people added side stepping (or strafing) in to their programs. I figured out how to move ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    banking, and side stepping

    I'm curious on how people added side stepping (or strafing) in to their programs. I figured out how to move around relative to the heading, and figured the side stepping would be easy to figure out based on the heading, but i'm having trouble.

    Code:
    glTranslatef(xpos,jump,zpos);
    
    //.... later in the code
    
                                    if(keys[VK_UP])
    				{
    				    zpos+=0.15f*cos(lookdir*pi/180.0);
    				    xpos+=0.15f*sin(lookdir*pi/180.0);
    				}
    				if(keys[VK_DOWN])
    				{
    				    zpos-=0.15f*cos(lookdir*pi/180.0);
    				    xpos-=0.15f*sin(lookdir*pi/180.0);
    				}
    				if(keys[VK_NEXT])
    				{
    				    xpos+=0.15f*cos(lookdir*pi/180.0);
    				    zpos+=0.15f*sin(lookdir*pi/180.0);
    				}
    				if(keys[VK_PRIOR])
    				{
    				    xpos-=0.15f*cos(lookdir*pi/180.0);
    				    zpos-=0.15f*sin(lookdir*pi/180.0);
    				}
    				if(keys[VK_LEFT])
    				{
    				    lookdir+=0.25f;
    				}
    				if(keys[VK_RIGHT])
    				{
    				    lookdir-=0.25f;
    				}
    I thought the side stepping (page up/page down keys) would have the zpos and xpos switched because of the strafing path being the perpendicular of the current heading. Well it works, but only for multiples of pi/2 headings. So what's wrong with the way i'm figuring out the side stepping?

    Then for banking, i want the camera, or viewport to tilt to the side (kind of like the screen is looking around a corner), but can't come up with a good approach to doing this. I thought I could tilt the entire "world" so to speak, but thought I'd have to rotate every object within the "world", and that seemed time consuming. Any ideas?

    Im using codeblocks with OpenGL.
    Last edited by scwizzo; 11-02-2007 at 08:33 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well it works, but only for multiples of pi/2 headings.
    I'm not sure if its the cause, but that sounds a bit like a rounding error.

    Heres some code I made for strafing in a raycasted game I was working on a while ago. Its not in C, but maybe it might help:
    Code:
    	If KeyDown(KEY_SHIFT)
    		If KeyDown(KEY_LEFT)	
    			mov_x# = pos_x# + ((-dir_y#) * mov_spd# *0.5)
    			mov_y# = pos_y# - ((-dir_x#) * mov_spd# *0.5)
    			If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
    			If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#	
    		Else If KeyDown(KEY_RIGHT)			
    			mov_x# = pos_x# + ((dir_y#) * mov_spd# *0.5)
    			mov_y# = pos_y# - ((dir_x#) * mov_spd# *0.5)	
    			If map(mov_x#, pos_y#) = 0 Then pos_x# = mov_x#
    			If map(pos_x#, mov_y#) = 0 Then pos_y# = mov_y#	
    		EndIf

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Thanks for the snippet, although it didn't give me much insight to my problem. However, I did figure it out. I was trying to use my look direction as the variable for the strafing, but needed to use the heading variable (which is 'heading = 360.0 - lookdir') to get the correct perpendicular direction (wow this stuff is confusing).

    I still don't have an idea to bank the view to the side. I thought maybe gluPerspective might work, but not quite. It would need to rotate, or pivot the camera view on all three axis.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    To strafe left and right you translate along your right vector by negative or positive amounts. To bank or roll as in an airplane you would want to rotate around your look vector.
    Last edited by VirtualAce; 11-07-2007 at 07:07 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21