Difference between revisions of "Vectors in PyGame"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Intro= Python can use tuples and lists to essentially represent a vector, however these will not allow you to do any real vector calculations. for example: <syntaxhighligh...")
 
(Bounce / reflect)
 
(5 intermediate revisions by the same user not shown)
Line 19: Line 19:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
test = math.Vector2(200,200)
 
test = math.Vector2(200,200)
 +
</syntaxhighlight>
 +
 +
You can access the x and y coordinate:
 +
 +
<syntaxhighlight lang=python>
 +
test = math.Vector2(200,200)
 +
print(test.x)
 +
print(test.y)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=Simple Calculations=
 
=Simple Calculations=
You can add or subtract vectors using standard calculations:
+
You can add, subtract, multiply or divide vectors using standard calculations:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
Line 34: Line 42:
 
Movement = math.Vector2(1,1)
 
Movement = math.Vector2(1,1)
 
Position += Movement
 
Position += Movement
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang=python>
 +
Position= math.Vector2(200,200)
 +
Movement *= 2
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 50: Line 63:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
test=math.Vector2(200,200)
 
test=math.Vector2(200,200)
test.length()
+
MAGNITUDE = test.length()
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
===Distance===
 
===Distance===
Line 60: Line 72:
 
test=math.Vector2(200,200)
 
test=math.Vector2(200,200)
 
DISTANCE = test.distance_to(math.Vector2(10,10))
 
DISTANCE = test.distance_to(math.Vector2(10,10))
 +
</syntaxhighlight>
 +
 +
=Bounce / Reflect=
 +
 +
Pygame.math does have a reflect and reflect_ip method to use a bounce, however you could easily do this yourself. If it is a horizontal bounce (from left or right) you have to negate the x coordinate:
 +
 +
<syntaxhighlight lang=python>
 +
test = math.Vector2((test.x*-1)  ,test.y)
 +
</syntaxhighlight>
 +
 +
For a vertical bounce, you need to negate the y coordinate:
 +
 +
<syntaxhighlight lang=python>
 +
test = math.Vector2(test.x , (test.y*-1))
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Latest revision as of 09:04, 18 July 2018

Intro

Python can use tuples and lists to essentially represent a vector, however these will not allow you to do any real vector calculations.

for example:

pos = [200,200]

Pygame has a built in maths module for 2D vector calcuations, you can import it:

import pygame.math as math

You can then create a new vector:

test = math.Vector2(200,200)

You can access the x and y coordinate:

test = math.Vector2(200,200)
print(test.x)
print(test.y)

Simple Calculations

You can add, subtract, multiply or divide vectors using standard calculations:

PositionA = math.Vector2(200,200)
PositionB = math.Vector2(500,300)
Distance = PositionB - PositionA
Position= math.Vector2(200,200)
Movement = math.Vector2(1,1)
Position += Movement
Position= math.Vector2(200,200)
Movement *= 2

Special Calculations

Dot Product

You can calculate the dot product between the vector and any other vector:

test=math.Vector2(200,200)
DOTPRODUCT = test.dot(math.Vector2(10,10))

Magnitude

Although pygame.math should contain a magnitude method, however this doesn't seem to be enabled. However the same calculation is also done by the length method:

test=math.Vector2(200,200)
MAGNITUDE = test.length()

Distance

Pygame.math has a built in method to calculate the distance between any two vectors:

test=math.Vector2(200,200)
DISTANCE = test.distance_to(math.Vector2(10,10))

Bounce / Reflect

Pygame.math does have a reflect and reflect_ip method to use a bounce, however you could easily do this yourself. If it is a horizontal bounce (from left or right) you have to negate the x coordinate:

test = math.Vector2((test.x*-1)  ,test.y)

For a vertical bounce, you need to negate the y coordinate:

test = math.Vector2(test.x , (test.y*-1))

Other Features

Pygame.math has other features and methods to use, check them out:

https://www.pygame.org/docs/ref/math.html