Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction
Game Movement by Rotation direction Game Movement by Rotation direction
Game Movement by Rotation direction
Go Back  Xtreme Visual Basic Talk > > > Game Movement by Rotation direction


Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2012, 12:52 AM
Pathogenix Pathogenix is offline
Newcomer
 
Join Date: May 2012
Location: Wisconsin
Posts: 5
Question Game Movement by Rotation direction


Okay so basically i'm trying to move the Player by rotation.

I have a perfect rotation system that works with a custom picturebox control that supports rotation that i have.

I'm completely stuck on this and i have no idea where i'd even start with the math behind it. Thanks in advance for help!
Attached Images
File Type: png 7e814827e0c1f4f8ef60d4b9b62d6c4e.png (37.5 KB, 16 views)
Reply With Quote
  #2  
Old 05-15-2012, 10:40 AM
passel's Avatar
passelGame Movement by Rotation direction passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

You take the Sin and Cos of the angle and multiply by the distance moved this frame to get the X and Y deltas to add to the current position.

From your diagram, I presume 0 degrees is straight up.
That would be negative Y, so when at 0 degrees you want Y to decrement but X not to change.

The Sine of 0 degrees is 0.0 so the Sine of the angle would be used to compute delta X.
The Cosine of 0 degrees is 1.0 so the Cosine of the angle would be used to compute delta Y. And since we want to go "up" we would subtract the delta Y value from the current position to move up.

The Sine of 90 degrees is 1.0 and since we want to move to the right for 90 degrees, then we would add the delta X value to our position to move right.

Now that we've determined whether we want to add or subtract the delta values for X and Y, we can write the code.

Since the Sine and Cosine values will return a value between -1 and 1, you would multiply that value by the distance you want to move for that frame to give you the delta X and delta Y values.

How you want to determine what that distance is, is up to you.
You may want a fixed distance per frame.
You may want a variable distance based on speed but assuming a fixed frame rate.
You may want a variable distance based on speed but allowing for a variable frame rate.

To allow for a variable frame rate, you would need to determine how much time passed since the last frame and multiply that by your speed to get the distance traveled this frame. Then multiply that distance by the sin/cos values to get the delta X and delta Y values to add to your current position.

So, that code could look like this: (note that Sin/Cos require angle in radians)
Code:
myObj.CurrentXPos += Math.Sin(CDbl(Heading * Math.PI / 180.0)) * (ElapsedTime * Speed)
myObj.CurrentYPos -= Math.Cos(CDbl(Heading * Math.PI / 180.0)) * (ElapsedTime * Speed)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 05-15-2012, 03:57 PM
Pathogenix Pathogenix is offline
Newcomer
 
Join Date: May 2012
Location: Wisconsin
Posts: 5
Default

Thank you so much for this!
The only things i don't understand is the ElapsedTime and the Heading Variables.
>.<

EDIT: I sort of understand now, but with the code you gave me its only returning 180 degrees.

Accell is a double and increments up by .25 when you hold a key to move.
Rotation is the degree of rotation that the picture box is.
even if i hold Right it moves down only a few pixels.(Accell Stops growing at 7)

Dim Xx As Integer = Xx + Math.Sin(CDbl(Math.PI / Rotation)) * accell
Dim Yy As Integer = Yy - Math.Cos(CDbl(Math.PI / Rotation)) * accell
PictureBox1.Location -= New Point(Xx, Yy)

If I'm correct this only moves a certain distance? I need to keep it moving while holding down a key.

Last edited by Pathogenix; 05-15-2012 at 04:15 PM.
Reply With Quote
  #4  
Old 05-16-2012, 02:54 PM
passel's Avatar
passelGame Movement by Rotation direction passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

You're not keeping track of the position.
You're declaring Xx and Yy each time you hit this code so Xx will be 0 and Yy will be 0.
You then add up to +-7 to that so your picturebox will always be within 7 pixels of 0,0.

If you declare Xx and Yy in the declarations area of the form, then they will be created once and hold their values (they can now accumlate the position over time) and be accessible to all routines in the form.

Also, since accell is presumed to be a Single {Edit: I now see you said it was a double} (so it can hold values like .25), the variables Xx and Yy should also be Singles so they can accumulate fractional amounts.
Otherwise, if you repeatedly add small values (less than .5) to an Integer, the Integer value will not change since it can only change in "whole" numbers.

Also, if Rotation is in degrees, dividing degrees into PI won't convert it to radians.
The value PI is the circumference of the circle measured in diameters lengths of the circle (there are 3.1415926…. diameter lengths around the circle).
The radius is ½ of the diameter, so there are twice as many radius lengths around the circle (i.e. two Pi radians).
You need to convert degrees into radians. So, the one scale (radians) is 0 to 2*Pi and the other scale (degrees) is 0 to 360.
To convert degrees to radians you multiple the degrees by (target scale)/(source scale), that is, degrees * (2*Pi/360), (which is equivalent to degrees * Pi/180).
Likewise, if you have radians and wanted to convert to degrees then you “flip” the scales (radians * 180/Pi).
It is common to declare a constant for these conversion values.

Code:
'In the declarations area at the top of the form code (below the Class statement)
Const DEG2RAD = Math.PI / 180.0
Const RAD2DEG = 180.0 / Math.PI
Dim Xx, Yy as Single   'hold the location of our moving object


'in the code where your movement is being done
    Xx += Math.Sin(Rotation * DEG2RAD) * accell
    Yy -= Math.Cos(Rotation * DEG2RAD) * accell
    PictureBox1.Location = New Point(Xx, Yy)

Here's a quick example base on your input. Normally you wouldn't move a picturebox around, you would move where you draw your object(s), but since you're currently geared for that, this moves a picturebox.
It draw a single line in the picturebox so you know which direction the box will head when you press the "Up" arrow key (up arrow changes accell value).
To turn left or right, you use the left and right arrow keys.

Just start a new project, and add a picturebox and a timer control to the form and paste this code in. Just a very simple example.
Code:
Public Class Form1
  Dim Xx, Yy As Single

  Dim Rotation As Single = 135.0
  Dim accell As Single
  Const DEG2RAD = Math.PI / 180.0
  Const RAD2DEG = 180.0 / Math.PI
  Dim TurnLeft, TurnRight, Accelerate, Decelerate As Boolean

  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
      Case Keys.Right : TurnRight = True
      Case Keys.Left : TurnLeft = True
      Case Keys.Up : Accelerate = True
      Case Keys.Down : Decelerate = True
    End Select
  End Sub

  Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    Select Case e.KeyCode
      Case Keys.Right : TurnRight = False
      Case Keys.Left : TurnLeft = False
      Case Keys.Up : Accelerate = False
      Case Keys.Down : Decelerate = False
    End Select
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.BorderStyle = BorderStyle.Fixed3D
    Timer1.Interval = 15
    Timer1.Start()
  End Sub

  Private Sub MoveObj()
    Xx += Math.Sin(Rotation * DEG2RAD) * accell
    Yy -= Math.Cos(Rotation * DEG2RAD) * accell
    PictureBox1.Location = New Point(Xx, Yy)
    PictureBox1.Invalidate()  'to redraw the direction line
  End Sub

  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If TurnRight Then
      Rotation += 5
      If Rotation >= 360 Then
        Rotation -= 360
      End If
    End If
    If TurnLeft Then
      Rotation -= 5
      If Rotation < 0 Then
        Rotation += 360
      End If
    End If

    If Accelerate Then
      If accell < 7 Then
        accell += 0.25
      End If
    Else
      If accell > 0 Then
        accell -= 0.25
        If accell < 0.2 Then
          accell = 0
        End If
      End If
    End If

    If Decelerate Then
      If accell > -7 Then
        accell -= 0.25
      End If
    Else
      If accell < 0 Then
        accell += 0.25
        If accell > -0.2 Then
          accell = 0
        End If
      End If
    End If

    MoveObj()
  End Sub

  Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    With PictureBox1
      e.Graphics.Clear(.BackColor)
      e.Graphics.TranslateTransform(.ClientSize.Width / 2, .ClientSize.Height / 2)
      e.Graphics.RotateTransform(Rotation)
      e.Graphics.FillRectangle(Brushes.Brown, -8, -8, 4, 16)
      e.Graphics.FillRectangle(Brushes.Brown, 4, -8, 4, 16)
      e.Graphics.FillEllipse(Brushes.Blue, -6, -6, 12, 12)
      e.Graphics.DrawLine(Pens.Black, 0, 0, 0, -25)
    End With
  End Sub
End Class
Edit: Added a couple of rectangles to the example do make the drawing a little more interesting. Now it sort of looks "tank" like with "treads" on the sides of the circle.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 05-16-2012 at 05:44 PM. Reason: Went ahead and added the Reverse case so object can move forward and backward.
Reply With Quote
  #5  
Old 05-16-2012, 05:24 PM
Pathogenix Pathogenix is offline
Newcomer
 
Join Date: May 2012
Location: Wisconsin
Posts: 5
Default

Okay i have almost everything working, I've implemented all the examples you generously gave me into the code i have perfectly!

Thank you!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction Game Movement by Rotation direction
Game Movement by Rotation direction
Game Movement by Rotation direction
 
Game Movement by Rotation direction
Game Movement by Rotation direction
 
-->