Space flight

kingtux
Posts: 2
Joined: Fri Aug 05, 2011 3:33 pm

Space flight

Post by kingtux »

I am making a space flight game using nova, a .net game engine I made using bullet and irrlicht. My problem is setting the mass, acceleration, etc. to work a space flight game. I want to be able to turn and have the space ship quickly start flying in that direction, instead of slowly slowing down and finally going in the right direction.

Here is the code where the problem is:

Code: Select all

Public Class Universe
    Inherits NovaUniverse

    Public IOManager As New IOStructManager
    Public TurnAcceleration As Single = 0.001
    Public TurnSpeed As Single = 0.01
    Public ShipAcceleration As Single = 0.05
    Dim ship As NovaRigidBodyBox

    Public Sub New()

        MyBase.New(1000, 1)

        IOManager.Types.Add(New KeyStructCreator)

        Worlds.Add(New NovaWorld(Me))
        'LoadStill(0, "Dodgefight.nova")

        'Generate asteroids

        For x = -100 To 100 Step 60
            For y = -100 To 100 Step 60
                For z = -100 To 100 Step 60
                    AddSphere(0, New Vector3(x + (Rnd() * 8 - 4), y + (Rnd() * 8 - 4), z + (Rnd() * 8 - 4)), 10 + (Rnd() * 16 - 8), 0, "asteroid")
                Next
            Next
        Next

        ship = AddBox(0, New Vector3(0, 0, 0), New Vector3(1, 0.5, 2), 0.01, "asteroid")
        ship.Node = -2
        Worlds(0).World.Gravity = New Vector3

        AddLight(0, New Vector3(0, 0, 0), New Colorf(1, 1, 1))

    End Sub

    Private Sub Universe_ReceivedMessage(ByVal bytes() As Byte) Handles Me.ReceivedMessage

        Dim structs = IOManager.ExtractStructs(bytes)

        Dim w = Worlds(0)

        If ship.Body.ActivationState = ActivationState.IslandSleeping Then ship.Body.Activate()

        Dim rotation = QuaternionToEuler(ship.Body.Orientation)
        Dim mat As New Core.Matrix
        mat.Rotation = Vector3ToVector3Df(rotation)

        For Each s As KeyStruct In structs

            If s.Key = 0 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(-s.Time * TurnAcceleration, 0, 0))))
            ElseIf s.Key = 1 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(s.Time * TurnAcceleration, 0, 0))))
            ElseIf s.Key = 2 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(0, -s.Time * TurnAcceleration, 0))))
            ElseIf s.Key = 3 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(0, s.Time * TurnAcceleration, 0))))
            ElseIf s.Key = 4 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(0, 0, -s.Time * TurnAcceleration))))
            ElseIf s.Key = 5 Then
                ship.Body.ApplyTorque(Vector3DfToVector3(mat.RotateVector(New Vector3Df(0, 0, s.Time * TurnAcceleration))))
            End If

        Next

    End Sub

    Public Overrides Sub Update()

        Dim v = ship.Body.AngularVelocity

        If v.X > TurnSpeed Then v.X = TurnSpeed
        If v.Y > TurnSpeed Then v.Y = TurnSpeed
        If v.Z > TurnSpeed Then v.Z = TurnSpeed

        If -v.X > TurnSpeed Then v.X = -TurnSpeed
        If -v.Y > TurnSpeed Then v.Y = -TurnSpeed
        If -v.Z > TurnSpeed Then v.Z = -TurnSpeed

        ship.Body.AngularVelocity = v

        If My.Computer.Clock.TickCount > LastUpdateTime + 30 Then '30 ms, approx 30 fps

            Dim m As New Core.Matrix  'BulletSharp.Matrix.RotationQuaternion(ship.Orientation)
            Dim r = Vector3ToVector3Df(QuaternionToEuler(ship.Orientation))
            'r = New Vector3Df(r.X, r.Y, r.z)
            m.Rotation = r
            Dim f As New Vector3Df(0, 0, -ShipAcceleration)
            m.TransformVector(f)

            ship.Body.ApplyCentralForce(Vector3DfToVector3(f))
        End If

        MyBase.Update()

    End Sub
eSalt
Posts: 15
Joined: Tue Jul 19, 2011 4:08 pm

Re: Space flight

Post by eSalt »

I haven't coded in VB before (but I do have experience in vc++.net), and my experiments with Irrlicht+Bullet were unfruitful, but it looks (at least to my inexperienced self) that TurnSpeed might be too low.

I didn't take much of a glance, I'm so distracted at the thought of how cool a .net+irrlicht+bullet combo would be. I tried to get irrBullet to work, but that, as I said, was unsuccessful. It certainly didn't help that my previous experiences were all with managed languages/scripting.

What license do you plan on putting your engine under when you're done? I can understand if you want to keep things proprietary. I'm just curious. :D

Edit: low, not slow... :wink: