⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movingentity.vb

📁 一个.Net下用VB编写的用于游戏的人工智能引擎
💻 VB
字号:
Public Class MovingEntity
    Inherits BaseGameEntity
    'protected:

    Protected m_vVelocity As Vector2D

    '//a normalized vector pointing in the direction the entity is heading. 
    Protected m_vHeading As Vector2D

    '//a vector perpendicular to the heading vector
    Protected m_vSide As Vector2D

    Protected m_dMass As Double

    '//the maximum speed this entity may travel at.
    Protected m_dMaxSpeed As Double

    '//the maximum force this entity can produce to power itself 
    '//(think rockets and thrust)
    Protected m_dMaxForce As Double

    '//the maximum rate (radians per second)this vehicle can rotate         
    Protected m_dMaxTurnRate As Double

    Public Sub New(ByVal position As Vector2D, ByVal radius As Double, ByVal velocity As Vector2D, ByVal max_speed As Double, ByVal heading As Vector2D, ByVal mass As Double, ByVal scale As Vector2D, ByVal turn_rate As Double, ByVal max_force As Double)
        MyBase.new(0, position, radius)
        m_vHeading = heading
        m_vVelocity = velocity
        m_dMass = mass
        m_vSide = m_vHeading.Perp()
        m_dMaxSpeed = max_speed
        m_dMaxTurnRate = turn_rate
        m_dMaxForce = max_force
        m_vScale = scale
    End Sub

    '//accessors
    Public Property Velocity() As Vector2D
        Get
            Return m_vVelocity
        End Get
        Set(ByVal Value As Vector2D)
            m_vVelocity = Value
        End Set
    End Property
    Public ReadOnly Property Mass() As Double
        Get
            Return m_dMass
        End Get
    End Property

    Public ReadOnly Property Side() As Vector2D
        Get
            Return m_vSide
        End Get
    End Property

    Public Property MaxSpeed() As Double
        Get
            Return m_dMaxSpeed
        End Get
        Set(ByVal Value As Double)
            m_dMaxSpeed = Value
        End Set
    End Property

    Public Property MaxForce() As Double
        Get
            Return m_dMaxForce
        End Get
        Set(ByVal Value As Double)
            m_dMaxForce = Value
        End Set
    End Property

    Public Function IsSpeedMaxedOut() As Boolean
        Return m_dMaxSpeed * m_dMaxSpeed >= m_vVelocity.LengthSQ()
    End Function
    Public ReadOnly Property Speed() As Double
        Get
            Return m_vVelocity.Length()
        End Get
    End Property
    Public ReadOnly Property SpeedSq() As Double
        Get
            Return m_vVelocity.LengthSQ()
        End Get
    End Property

    Public Property Heading() As Vector2D
        Get
            Return m_vHeading
        End Get
        '//------------------------- SetHeading ----------------------------------------
        '//
        '//  first checks that the given heading is not a vector of zero length. If the
        '//  new heading is valid this fumction sets the entity's heading and side 
        '//  vectors accordingly
        '//-----------------------------------------------------------------------------

        Set(ByVal Value As Vector2D)
            m_vHeading = Value

            '//the side vector must always be perpendicular to the heading
            m_vSide = m_vHeading.Perp()
        End Set
    End Property

    '//--------------------------- RotateHeadingToFacePosition ---------------------
    '//
    '//  given a target position, this method rotates the entity's heading and
    '//  side vectors by an amount not greater than m_dMaxTurnRate until it
    '//  directly faces the target.
    '//
    '//  returns true when the heading is facing in the desired direction
    '//-----------------------------------------------------------------------------
    Public Function RotateHeadingToFacePosition(ByVal target As Vector2D) As Boolean
        Dim toTarget As Vector2D = Vector2D.Vec2DNormalize(target.Minus(m_vPos))

        '//first determine the angle between the heading vector and the target
        Dim angle As Double = Math.Acos(m_vHeading.Dot(toTarget))

        '//return true if the player is facing the target
        If (angle < 0.00001) Then Return True

        '//clamp the amount to turn to the max turn rate
        If (angle > m_dMaxTurnRate) Then angle = m_dMaxTurnRate

        '//The next few lines use a rotation matrix to rotate the player's heading
        '//vector accordingly
        Dim RotationMatrix As C2DMatrix

        '//notice how the direction of rotation has to be determined when creating
        '//the rotation matrix
        RotationMatrix.Rotate(angle * m_vHeading.Sign(toTarget))
        RotationMatrix.TransformVector2Ds(m_vHeading)
        RotationMatrix.TransformVector2Ds(m_vVelocity)

        '//finally recreate m_vSide
        m_vSide = m_vHeading.Perp()

        Return False
    End Function

    Public Property MaxTurnRate() As Double
        Get
            Return m_dMaxTurnRate
        End Get
        Set(ByVal Value As Double)
            m_dMaxTurnRate = Value
        End Set
    End Property
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -