#### Archived

This topic is now archived and is closed to further replies.

# 2D: Strafing

This topic is 5546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m creating a rolling ball game and I''d like to add a strafe feature so that the player can strafe left and right. I''m using XVelocity, YVelocity, Acceleration, and Direction to move the ball. Direction is an angle at which the ball moves according to which way the ball is facing. The code below is called every frame to adjust the ball''s position based on it''s velocity, acceleration, and direction. I''d like the ball to strafe relative to it''s direction, not only on the X-axis.
''If ball is being moved (''Up'' key pressed)
If Accel = True Then
If XVelo >= 15 Then
''Set a maximum XVelo
XVelo = 15
Else
''If not, retain current XVelo
XVelo = XVelo + Acceleration
End If

If YVelo >= 15 Then
''Set a maximum YVelo
YVelo = 15
Else
''If not, retain current YVelo
YVelo = YVelo + Acceleration
End If
Else
''Ball is not being moved (''Up'' key depressed)
If XVelo = 0 Then
''Keep the ball still
XVelo = 0
ElseIf XVelo > 0 Then
''Slow the ball down
XVelo = XVelo - Acceleration
End If

If YVelo = 0 Then
YVelo = 0
ElseIf .YVelo > 0 Then
YVelo = YVelo - Acceleration
End If
End If

XAmt = (PtrX  - XPos) / PtrRadius
YAmt = (PtrY  - YPos) / PtrRadius

XAmt = XPos + XAmt * XVelo
YAmt = YPos + YAmt * YVelo

If XAmt <= 0 Then XAmt = 0
If YAmt <= 0 Then YAmt = 0

XPos = XAmt
YPos = YAmt

''Create a point "in front" of the ball so that when it accelerates, it will move towards this point
PtrX = (XPos + Cos(Direction * (PI / 180)) * 25)
PtrY = (YPos + Sin(Direction * (PI / 180)) * 25)


##### Share on other sites
Well, strafing would occur at a right angle to the direction of movement. Easiest way to get these two vectors would be to use an artificial surface normal like (0,0,1), and then take the cross product with the direction. If you do DxN, then you get the right strafe, and NxD would get you the left strafe.

EDIT: Forgot to mention that your other two vectors for the cross product operation would just be (x,y,0). If you need a little more information on the cross product, then a good Google search will provide you with more information than I could ever offer

[edited by - Zipster on October 7, 2003 3:49:45 PM]

##### Share on other sites
Is there a simpler way to do this? I just thought that I''d be able to modify XAmt and YAmt whenever the ball is strafing.

##### Share on other sites
To straff simply add/substract 90 degrees from your facing angle when calculating the movement.

	if(keys[VK_UP])	{		xpos+=(float)sin(yrot*3.14/180.0)*speed;		zpos-=(float)cos(yrot*3.14/180.0)*speed;	}	if(keys[VK_DOWN])	{		xpos-=(float)sin(yrot*3.14/180.0)*speed;		zpos+=(float)cos(yrot*3.14/180.0)*speed;	}	if(keys[VK_LEFT])	{		xpos+=(float)sin((yrot-90)*3.14/180.0)*speed;		zpos-=(float)cos((yrot-90)*3.14/180.0)*speed;	}	if(keys[VK_RIGHT])	{		xpos+=(float)sin((yrot+90)*3.14/180.0)*speed;		zpos-=(float)cos((yrot+90)*3.14/180.0)*speed;	}

Ben

[ IcarusIndie.com | recycledrussianbrides.com | Got Linux? ]

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013645
×