November 25, 2020, 08:26:33 AM

### Author Topic: Simple Vectors in Blitz3D  (Read 2529 times)

#### 3DzForMe ##### Simple Vectors in Blitz3D
« on: July 19, 2017, 05:26:58 AM »
Just in case peeps are struggling with which language to code their asteroids style game in, here's some sweet code that works well demonstrating one way to skin the 'Vectors' cat within blitz 3D / blitz basic Code: [Select]
`;FULL credit to betties art for the following functions:;    http://www.bettiesart.com/tc/blitz/; Example of space physics using vector translation (Method A); ---------------------------------------------------------------------------- ;; Demonstrates the use of angle, thrust, heading and velocity;Function prog4 ()    Graphics 640 , 480    SetBuffer BackBuffer()     ; Keyboard scancodes KeyCursorUp%    = 200 KeyCursorDown%  = 208 KeyCursorLeft%  = 203 KeyCursorRight% = 205 KeyEscape%      =   1 KeySpace%       =  57     ; Craft specifications ThrustMax# = 0.1  ; Maximum thrust speed SpeedMax# = 5     ; Maximum allowed velocity RotateMax# = 4    ; Maximum rotation speed (degrees)     ; Craft related    PosX# = GraphicsWidth() / 2   ; Start at center of screen    PosY# = GraphicsHeight() / 2  ;    Thrust#  = 0  ; Current thrust speed - in the direction of where the ship is pointing at    Angle%   = 0  ; Current angle (rotation) - where the ship is pointing at    Speed#   = 0  ; Current actual speed of the ship    Heading% = 0  ; Current heading - where the ship is moving towards (degrees)     ; Logic related    Local Thrusting% = False  ; Thrusts forwards/backwards when True, automatically decelerates thrust when False    Local VelocityX# = 0  ; Temporary variables for calculating the new velocity vector    Local VelocityY# = 0  ;     Repeat Cls If KeyDown( KeyCursorLeft ) Then Angle = Angle - RotateMax  ; Rotate anti-clockwise If KeyDown( KeyCursorRight ) Then Angle = Angle + RotateMax  ; Rotate clockwise If Angle < 0 Then Angle = Angle + 360  ; Make sure angle is in range of 0-359 (shift) If Angle > 359 Then Angle = Angle - 360 Thrusting = False  ; Automatically decelerate thrust - reset thrusting flag If KeyDown( KeyCursorUp ) Then  ; Cursor up held down Thrust = ThrustMax  ; Start thrusting forwards at max speed Thrusting = True  ; Do not decelerate thrust EndIf If KeyDown( KeyCursorDown ) Then  ; Cursor down held down Thrust = -ThrustMax  ; Start thrusting backwards at max speed Thrusting = True  ; Do not decelerate thrust EndIf If Not Thrusting Then Thrust = 0  ; Automatically decelerate thrusting when not thrusting (on request)       ; Note:      ;   Speed & Heading = Current velocity vector [ VelocityX VelocityY ]      ;   Thrust & Angle = Current thrust vector [ ThrustX ThrustY ] VelocityX = VectorX( Speed , Heading ) + VectorX( Thrust , Angle )  ; The new velocity vector is the sum of VelocityY = VectorY( Speed , Heading ) + VectorY( Thrust , Angle )  ; the current velocity and thrust vector Speed = VectorDistance( VelocityX , VelocityY )   ; Translate back to speed and heading Heading = VectorAngle( VelocityX , VelocityY )  ; If Speed > SpeedMax Then Speed = SpeedMax  ; Limit speed to maximum If KeyDown( KeySpace ) Then Speed = 0  ; Handbrake PosX = PosX + VectorX( Speed , Heading )  ; Update horizontal position with current horizontal velocity inc. If PosX < 0 Then PosX = PosX + GraphicsWidth()  ; Warp if off-screen If PosX >= GraphicsWidth() Then PosX = PosX - GraphicsWidth() PosY = PosY + VectorY( Speed , Heading )  ; Same for vertical If PosY < 0 Then PosY = PosY + GraphicsHeight() If PosY >= GraphicsHeight() Then PosY = PosY - GraphicsHeight() Color 0 , 127 , 255  ; Additional information Oval PosX - 10 , PosY - 10 , 21 , 21 , True Text 0 , 0 * FontHeight() , "X: " + Int(PosX) + " Y: " + Int(PosY) Color 127 , 255 , 127 Line PosX , PosY , PosX + VectorX( 10 , Heading ) , PosY + VectorY( 10 , Heading ) Text 0 , 1 * FontHeight() , "Speed: " + Int(Speed) + " Heading: " + Heading Color 255 , 255 , 255 Line PosX , PosY , PosX + VectorX( 20 , Angle ) , PosY + VectorY( 20 , Angle ) Text 0 , 2 * FontHeight() , "Thrust: " + Thrusting + " Angle: " + Angle Flip    Until KeyDown( KeyEscape )     EndGraphics    End;End Function ;FULL credit to betties art for the following functions: ;    http://www.bettiesart.com/tc/blitz/ ;=======================================================  ; For those of you (including me)    ; who don't like doing all the maths    ; every time and time again :P    ;    ;= Get horizontal size of vector using distance and angle Function VectorX# ( Distance# , Angle# ) Return Sin ( Angle ) * DistanceEnd Function    ;= Get vertical size of vector using distance and angleFunction VectorY# ( Distance# , Angle# ) Return Sin ( Angle - 90 ) * DistanceEnd Function    ;= Get true length of a vectorFunction VectorDistance# ( X# , Y# ) Return Sqr ( X * X + Y * Y )End Function    ;= Get true angle of a vectorFunction VectorAngle# ( X# , Y# ) Return -ATan2 ( X , Y ) + 180End Function    ;`
happy coding http://www.gpsrunner.co.uk/8bitfun.html

#### MagosDomina ##### Re: Simple Vectors in Blitz3D
« Reply #1 on: September 08, 2017, 04:28:52 PM »
Most excellent, thanks for posting this!
Central hub for my Game Design related activities.
http://www.magosdomina.com/

Windows 7 PRO 64-bit, 48Gb RAM, CPU: Dual Hexacore Intel Xeon 3.33 GHZ, Nvidia Quadro 5800FX 4Gb x 2 SLI.

#### 3DzForMe ##### Re: Simple Vectors in Blitz3D
« Reply #2 on: September 08, 2017, 05:09:36 PM »
Glad you appreciated it happy coding http://www.gpsrunner.co.uk/8bitfun.html

#### RobFarley ##### Re: Simple Vectors in Blitz3D
« Reply #3 on: September 12, 2017, 03:33:03 PM »
In Blitz3D, rather than using 2D maths, this stuff is much easier (so this is going from memory when I programmed blitz about 10 years ago!)... Just create an entity at 0,0,0 call it origin, create an entity for your thrust vector, then you can apply a force to your thrust entity be it a transpose or rotate and move... The resultant movement will be the x / z coordinate of your thrust vector. To add dampening, simply point it at the origin and move it forwards... faster it moves towards the origin the more dampening you have.

Something like that anyway!
Cheers, Rob