Convert Mouse/Keyboard Input...

Started by
0 comments, last by ViperG 17 years, 6 months ago
I would like an explanation of the math/process to convert relative mouse input (*RELATIVE*) into actor rotation updates and keyboard input into position updates. I want to do the following: 1) Get keyboard/mouse input. 2) Update actor rotation/position (based on input). 3) Make camera chase actor (third step not need explanation yet) Here is the example of my code so far:

Private Sub GameLoop()
    Do
        DoEvents

        CheckInput
        UpdatePlayer
        ChasePlayer
        
        objTVEngine.Clear
            objTVAtmosphere.Skybox_Render
            objTVLandscape.Render
        objTVEngine.RenderToScreen
    Loop Until (m_boolLoop = False)
    
    Unload Me
End Sub

Private Sub CheckInput()
    Dim lngMouseX As Long, lngMouseY As Long
    Dim intButton1 As Integer, intButton2 As Integer
    Dim lngMouseRoll As Long, bytKeyStates() As Byte
    
    objTVInputEngine.GetKeyPressedArray bytKeyStates()
    If bytKeyStates(TV_KEY_W) Then
        'move forward
    End If
    
    If bytKeyStates(TV_KEY_A) Then
        'rotate left
    End If
    
    If bytKeyStates(TV_KEY_S) Then
        'move backward
    End If
    
    If bytKeyStates(TV_KEY_D) Then
        'rotate right
    End If

    objTVInputEngine.GetMouseState lngMouseX, lngMouseY, intButton1, intButton2, , lngMouseRoll
    If intButton1 Then
        'rotation
    End If
    
    If intButton2 Then
        'rotation
    End If
    
    If intButton2 And bytKeyStates(TV_KEY_A) Then
        'strafe left
    End If
    
    If intButton2 And bytKeyStates(TV_KEY_D) Then
        'strafe right
    End If
End Sub

Private Sub UpdatePlayer()

End Sub

Private Sub ChasePlayer()

End Sub
(btw relative mouse is the difference in oldposition from new position, relativex = oldmousex - newmousex, relativey = oldmousey - newmousey) I don't want anyone to just copy/paste code that will work. I want to understand how to use math to convert the mouse input and keyboard input into rotation/position updates on my actor.
Advertisement
[Er edit my example is mostly C language. But the Cosine/Sin functions should help you out]

well it depends.

with windows you can use windows messages to recieve input.

But I'd recommend using GetAsyncKeyState(); for keyboard and POINT mouse; for the mouse.

It also depends on if your doing 2d or a 3d game here.

But to find the angle of the player so you can move him, you need to use COS and Sin functions.

Plus you need to give the player a heading. 0-360 degrees.

lets say your player is facing 90 degrees.

Sin(90) = 1;
Cos(90) = 0;

Sin is your x, and Cos is youy y. so

Player.Location+= Sin(Player.angle)*Player.moveamount;
Player.Location+= Cos(Player.angle)*Player.moveamount;

So if you think about this. If the player is facing 90 degrees, and you hit forward on your keyboard, only is X coordinate would change, his y would not.

If he was facing 0 degrees (straight up, or north, depending on your game world)
he would only move on the Y, and X wouldn't change.

if he was facing 180, then he is moving Negative Y, and X wouldnt change

Hope that makes sense, you need to at least know geometry to understand that.

You can also use Matrices (Matrix Math to do all of this stuff too, but that is Algebra 2/Trig stuff)

Also keep in mind if your programing in C, Cos And Sin return Radians. so to convert that you go

180 / 3.1415926

so Sin(Player.heading * (180 / 3.1415926));
and you have to do the same thing with Cosine.
Black Sky A Star Control 2/Elite like game

This topic is closed to new replies.

Advertisement