Jump to content
  • Advertisement


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


2D: Strafing

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

If you intended to correct an error in the post then please contact us.

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
        ''If not, retain current XVelo
        XVelo = XVelo + Acceleration
    End If
    If YVelo >= 15 Then
        ''Set a maximum YVelo
        YVelo = 15
        ''If not, retain current YVelo
        YVelo = YVelo + Acceleration
    End If
''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 this post

Link to post
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 this post

Link to post
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 this post

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



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

Will Post For Food

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!