Question simple maths?

meandi

New member
Joined
Oct 21, 2009
Messages
3
Programming Experience
Beginner
VB.NET:
Imports Microsoft.DirectX
'Imports Microsoft.DirectX.DirectInput
Imports Microsoft.DirectX.Direct3D
'Imports Microsoft.DirectX.DirectSound
Imports Microsoft.DirectX.AudioVideoPlayback
Imports Microsoft.DirectX.DXHelp
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

    'keyboard
    Private KB As New DirectInput.Device(DirectInput.SystemGuid.Keyboard)

    'graphicsdevice
    Private pars As New PresentParameters
    Private GD As Device

    'sprites
    Private _ship As Sprite = New Sprite(GD)

    'textures
    Private ship As Texture

    Private turn As Single = 0
    Private turn2 As Single = 0
    Private x As Integer = 0
    Private y As Integer = 0
    Private z As Integer = 1
    Private v As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'graphicsdevice
        pars.Windowed = True
        pars.SwapEffect = SwapEffect.Discard
        GD = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, pars)

        'textures
        ship = TextureLoader.FromFile(GD, System.IO.Directory.GetCurrentDirectory & "\Graphs\ship0.bmp", 256, 256, D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, Color.Magenta.ToArgb)

        'sprites
        _ship = New Sprite(GD)

        'keyboard
        KB.SetCooperativeLevel(Me, DirectInput.CooperativeLevelFlags.Background Or DirectInput.CooperativeLevelFlags.NonExclusive)

        Me.Show()

        KB.Acquire()
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        KB.Dispose()
        KB = Nothing

        GD.Dispose()
        _ship.Dispose()
        ship.Dispose()

    End Sub

    Private Sub Keys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keys.Tick

        Dim State As DirectInput.KeyboardState
        State = KB.GetCurrentKeyboardState

        If State.Item(Microsoft.DirectX.DirectInput.Key.Escape) Then
            Me.Close()
        End If

        If State.Item(Microsoft.DirectX.DirectInput.Key.W) Then
            y -= 50
            z = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2))
            turn2 = Math.Acos(y / z) + Math.PI - turn
        End If

        If State.Item(Microsoft.DirectX.DirectInput.Key.S) Then
            y += 50
            z = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2))
            turn2 = Math.Acos(y / z) + Math.PI - turn
        End If

        If State.Item(Microsoft.DirectX.DirectInput.Key.A) Then
            turn -= 0.1257
            If (turn < 0) Then
                turn += 6.285
            End If
            x = z * Math.Sin(turn2 + turn - Math.PI)
            y = z * Math.Cos(turn2 + turn - Math.PI)
        End If

        If State.Item(Microsoft.DirectX.DirectInput.Key.D) Then
            turn += 0.1257
            If (turn >= 6.285) Then
                turn -= 6.285
            End If
            x = z * Math.Sin(turn2 + turn - Math.PI)
            y = z * Math.Cos(turn2 + turn - Math.PI)
        End If

        Call Form1_Paint(Nothing, Nothing)

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        If GD Is Nothing Then Return

        GD.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0F, 0)

        GD.BeginScene()

        _ship.Begin(SpriteFlags.AlphaBlend)

       

        _ship.Draw2D(ship, Rectangle.Empty, New System.Drawing.Rectangle(0, 0, 40, 40), New Point(125, 125), turn, New Point(x, y), Color.White)
       



        GD.EndScene()
        GD.Present()
    End Sub

End Class

when i use w, a, s and d buttons the ship goes around like it should, but only for half of the time other times the ship jumps around randomly (I can't figure out the patern)

when i use front (w or s) and turn (a or d) the same time it goes in big circles around the center (this i could fix)

question:

why does it sometimmes randomly jump (not doing what it suposed to do) when i push one button at a time
 
Last edited:
I'll have to wait till I get home, to look at some of my code, to get a clear answer for you but I wanna make sure I understand your code first.

What is turn2 and turn? Your code looks pretty similar to the basic code to control movement, but I've never seen it with two variables for the angle(theta)
 
two turns

turn is the rotation of the ship

turn2 is an aid in pointing out the position (u can do without it)

I needed this cause vb.net uses a rotating 'coordinate system' and i wanted to be able to turn the picture appart from the position.

You can see turn2 only goes to draw2d via x and y and that only for buttons a and d (while rotating)


Rotating in a rotating coordinate system.:cool:

If u can use the rotationCenter that would be nice, it would make things shorter.
Wich reminds me of the fact that i put 125,125 in there cause it's the only thing that works. (for now)
 
if anyone want's to recreate this all you have to do is create a form with a timer (it is set to 20ms wich gives it 50fps), make sure u have the pictures(some random picture about 40x40) and don't forget to add references to directx and such
(and the code above...)

I use visual studio 2008
 
Back
Top