Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Vectors  (Read 2826 times)
zabriel
Community Member
*
Posts: 30


« on: March 01, 2003, 06:29:29 PM »

Is there a built in function to convert an angle to a vector?
Logged
Canning
Community Member
*
Posts: 592


« Reply #1 on: June 05, 2010, 05:19:41 AM »

I am after the same code.

Any luck with it?
Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
jviper
Community Member
*
Posts: 2130

Discipline in training


« Reply #2 on: June 05, 2010, 07:06:49 AM »

Vec.x = Dist * cos( angle )
Vec.y = Dist * sin( angle )
Logged

JAbstract.....Don't just imagine, make it happen!
Mithrandir
Community Member
*
Posts: 325


« Reply #3 on: June 05, 2010, 11:28:55 AM »

If you need to do more rotation you can use:
MoveAroundPoint to rotate a point around origin along Y and X axis (Horizontal and vertical angle)
(Good for flying spectator camera)

You can also crate arbitrary rotation matrix using TVMatrixRotationAxis, TVMatrixRotationQuaternion, TVMatrixRotationX, TVMatrixRotationY, TVMatrixRotationZ, TVMatrixRotationYawPitchRoll.
Then you multiply this matrix by a vector and the vector will be rotated by the matrix. So if you have Vector3(1,0,0) and multiply matrix by it, you'll get direction.
(Good for planes where rotation is stored in matrix or quaternion)

But if you just need 2D direction use jviper's  code. That is the simplest way.
(Good for FPS camera)
« Last Edit: June 05, 2010, 12:58:05 PM by Mithrandir » Logged
Canning
Community Member
*
Posts: 592


« Reply #4 on: June 05, 2010, 08:23:02 PM »

Code:
        Dim CurrentDirection As D3DVECTOR
   
        CurrentDirection.x = 10 * Cos(TankAngleY)
        CurrentDirection.z = 0
        CurrentDirection.y = 10 * Sin(TankAngleY)

        If GetDistance3D(TankPosition.x, 0, TankPosition.z, TankDestination.x, 0, TankDestination.z) > 2 Then
       
       
            TankPosition = VAdd(TankPosition, VScale(CurrentDirection, TV3D.TimeElapsed * 0.1))
            TankPosition.y = Land.GetHeight(TankPosition.x, TankPosition.z) + 10
       
            Tank.SetPosition TankPosition.x, TankPosition.y, TankPosition.z
   
        End If

This will not work... is it because I am trying to use 2d direction rather than 3d direction?
Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
Mithrandir
Community Member
*
Posts: 325


« Reply #5 on: June 06, 2010, 08:42:57 AM »

It should be:
Code:
CurrentDirection.x = 10 * Cos(TankAngleY)
CurrentDirection.y = 0
CurrentDirection.z = 10 * Sin(TankAngleY)

Also make sure you are using right angle system. Native math libraries in all programming languages use radians. If you have the angle in degrees it wont work properly. TV uses radians by default.
Logged
Canning
Community Member
*
Posts: 592


« Reply #6 on: June 06, 2010, 11:23:54 PM »

Mithrandir:, Can you please help me in this post?

http://www.truevision3d.com/forums/tv3d_sdk_65/check_movement-t20154.0.html
Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
Mithrandir
Community Member
*
Posts: 325


« Reply #7 on: June 07, 2010, 10:33:04 AM »

Code:
Imports MTV3D65
Public Class Form1
    Dim Tank As TVMesh ' Moving tank
    Dim Land As TVLandscape ' Landscape (for mouse picking)
    Dim TargetCursor As TVMesh ' Sphere marking target point
    Dim TV As TVEngine
    Dim IE As TVInputEngine
    Dim Scene As TVScene
    Dim glb As TVGlobals
    Dim Mth As TVMathLibrary

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TV = New TVEngine
        TV.Init3DWindowed(Me.Handle.ToInt32)
        glb = New TVGlobals
        Mth = New TVMathLibrary
        Scene = New TVScene
        ' Create empty landscape
        Land = Scene.CreateLandscape
        Land.CreateEmptyTerrain(CONST_TV_LANDSCAPE_PRECISION.TV_PRECISION_LOW, 2, 2, -256, 0, -256)
        ' Create tank mesh
        Tank = Scene.CreateMeshBuilder
        Tank.CreateTeapot()
        Tank.SetScale(10, 10, 10)
        ' Create cursor mesh
        TargetCursor = Scene.CreateMeshBuilder()
        TargetCursor.CreateSphere(3)
        TargetCursor.SetColor(glb.RGBA(0, 1, 0, 1))
        ' Create input engine
        IE = New TVInputEngine
        IE.Initialize()
        ' Set camera
        Scene.SetCamera(50, 150, 50, 0, 0, 0)
        ' Declare variables
        Dim Target As TV_3DVECTOR
        Dim Angle As Double
        Dim Direction As TV_3DVECTOR
        Dim Position As TV_3DVECTOR
        Dim MouseX As Integer, MouseY As Integer

        Me.Show()
        Do While Not IE.IsKeyPressed(CONST_TV_KEY.TV_KEY_ESCAPE)
            TV.Clear()
            ' Move target on mouse click
            If (IE.IsMouseButtonPressed(0)) Then
                IE.GetMousePosition(MouseX, MouseY)
                Dim Coll As TVCollisionResult = Land.MousePick(MouseX, MouseY)
                Target = Coll.GetCollisionImpact()
                TargetCursor.SetPosition(Target.x, Target.y, Target.z)
            End If
            ' If too far from target, steer tawards target
            If Mth.GetDistanceVec3D(Position, Target) > 0.5 Then
                ' Calculate angle towards target
                Dim AngleToTarget As Double = Mth.ATan2(Position.x - Target.x, Position.z - Target.z)
                ' Calculate relative angle between current angle/direction and desired angle towards target
                Dim RelativeAngle As Double = Angle - AngleToTarget
                ' Make sure angle stays in interval <-pi;pi>
                Do While RelativeAngle > Math.PI
                    RelativeAngle = RelativeAngle - (2.0 * Math.PI)
                Loop
                Do While RelativeAngle < -Math.PI
                    RelativeAngle = RelativeAngle + (2.0 * Math.PI)
                Loop
                ' Change current angle according to relative diraection tawards target
                If RelativeAngle > 0 Then
                    Angle += 0.005
                End If
                If RelativeAngle < 0 Then
                    Angle -= 0.005
                End If
                ' Calculate direction from angle
                Direction = glb.Vector3(Math.Cos(Angle) * 0.1, 0, Math.Sin(Angle) * 0.1)
                ' Update position
                Position = Position + Direction
                ' Set mesh rotation and position
                Tank.SetRotation(0, -Angle, 0)
                Tank.SetPosition(Position.x, Position.y, Position.z)

            End If
            ' Don't render the landscape
            Land.Enable(False)
            Scene.RenderAll(True)
            Land.Enable(True)
            TV.RenderToScreen()
            Windows.Forms.Application.DoEvents()
        Loop
        Me.Close()
    End Sub
End Class

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks