Weekly Qbasic and Qb64 Lesson Topics
April 18, 2024, 06:50:27 pm
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

Vector movement.

Pages: [1]
  Print  
Author Topic: Vector movement.  (Read 1595 times)
Unseen Machine
Post Demos
*
Posts: 46


« on: April 27, 2011, 04:47:14 pm »

Part 1 :

Code:
'// Vector movement tutorial 1. By John Onyon a.k.a Unseen Machine
'// This demo makes 2 circles orbit around the screen and only requires QB64.
'//
'// This demo is as simple as possible, if you want something more advanced see tutorial 2.
'//
'// Vector movement is used for moving objects based on their direction and speed.
'// It can be used for moving all sorts of objects such as space ships, bullets and falling objects.
'//
'// It is an essential tool to have in your game programming arsenal.
'// MATH NOTE : 4 * ATN(1) = Pi, so 2 * ATN(1) = half Pi
'//
'// So, set up a QB64 graphics screen so we can draw stuff.
SCREEN _NEWIMAGE(600, 400, 32)
'//
'// In it's most basic form a vector has Speed and Direction, these are best as real numbers rather than integers
'// so you can have a finer level of control of the object.

DIM Ball_1_Speed, Ball_1_Direction, Ball_2_Speed, Ball_2_Direction

Ball_1_Speed = 3
Ball_1_Direction = 1 * ATN(1)
Ball_2_Speed = 5
Ball_2_Direction = 7 * ATN(1)

'// The ball's will also need X and Y variables to hold their positions. These should be single point variables aswell.

DIM Ball_1_X, Ball_1_Y, Ball_2_X, Ball_2_Y

Ball_1_X = 250
Ball_1_Y = 200
Ball_2_X = 300
Ball_2_Y = 200


DO

  _LIMIT 60 '// Limit the FPS

  CLS '// Clear the screen

  '// Draw the balls
  CIRCLE (Ball_1_X, Ball_1_Y), 25, _RGB(255, 255, 0)
  CIRCLE (Ball_2_X, Ball_2_Y), 15, _RGB(255, 0, 0)

  '// Keep increasing the balls rotation to make them orbit.
  Ball_1_Direction = Ball_1_Direction + .05
  Ball_2_Direction = Ball_2_Direction - .05

  '// Update the balls position
  UpdateVector Ball_1_X, Ball_1_Y, Ball_1_Speed, Ball_1_Direction
  UpdateVector Ball_2_X, Ball_2_Y, Ball_2_Speed, Ball_2_Direction

  _DISPLAY '// Update the screen
LOOP

SUB UpdateVector (X, Y, Speed, Direction)
X = X - SIN(Direction) * Speed
Y = Y - COS(Direction) * Speed
END SUB
Report Spam   Logged

Share on Facebook Share on Twitter

Unseen Machine
Post Demos
*
Posts: 46


« Reply #1 on: April 27, 2011, 05:04:04 pm »

Part 2 :

Code:
'// Vector movement tutorial 2. By John Onyon a.k.a Unseen Machine
'// This demo makes 2 circles orbit around the screen and only requires QB64.
'//
'// This demo is more advanced than tutorial 1.
'//
'// MATH NOTE : 4 * ATN(1) = Pi, so 2 * ATN(1) = half Pi
'//
'// So, set up a QB64 graphics screen so we can draw stuff.
SCREEN _NEWIMAGE(600, 400, 32)
'//
'// As everything that uses vector movement has x and y positions, speed and direction it is sensible to
'// put all these vairbales in one TYPE array. This makes it easier to both create and use the vaibles in your code.

TYPE Vector
  X AS SINGLE
  Y AS SINGLE
  Dir AS SINGLE
  Speed AS SINGLE
END TYPE

'// So to make two vectors now all you have to do is DIM them, as we want two, lets use an array.
DIM Ball_Vector(2) AS Vector

'// As we now have a TYPE array, it would be sensible to make a sub that loads the basic information of the type.
'// This makes for less typing in the long run.

NewVector Ball_Vector(0), 200, 200, 2, 2 * ATN(1)
NewVector Ball_Vector(1), 400, 200, 2, 6 * ATN(1)

DO

  _LIMIT 60 '// Limit the FPS

  CLS '// Clear the screen

  '// Draw the balls
  CIRCLE (Ball_Vector(0).X, Ball_Vector(0).Y), 25, _RGB(255, 0, 0)
  CIRCLE (Ball_Vector(1).X, Ball_Vector(1).Y), 25, _RGB(255, 255, 0)

  '// Keep increasing the balls rotation to make them orbit.
  Ball_Vector(0).Dir = Ball_Vector(0).Dir - .05
  Ball_Vector(1).Dir = Ball_Vector(1).Dir + .05

  '// Update the balls position. Now with a sub that just needs you to pass the vector rather than 4 variables.
  UpdateVector Ball_Vector(0)
  UpdateVector Ball_Vector(1)

  _DISPLAY '// Update the screen
LOOP


SUB UpdateVector (Vector AS Vector)
Vector.X = Vector.X - SIN(Vector.Dir) * Vector.Speed
Vector.Y = Vector.Y - COS(Vector.Dir) * Vector.Speed
END SUB


SUB NewVector (Vector AS Vector, X, Y, Speed, Direction)
Vector.X = X
Vector.Y = Y
Vector.Dir = Direction
Vector.Speed = Speed
END SUB
Report Spam   Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy