• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
silverphyre673

Is there a built-in Python vector class?

6 posts in this topic

I'm experimenting with flocking bots, and I don't want to go to the trouble of coming up with a vector class. Is there something that works well already out there? Thanks.
0

Share this post


Link to post
Share on other sites
If you want, here's the vector class that I wrote. No guarantees on anything, but it's working great for me. It's only for 2D vectors, but I'm guessing that that will be enough for your work. Note that I also define a class Point to be the same as Vector, which you might not want.

import math
class Vector:
'Represents a 2D vector.'
def __init__(self, x = 0, y = 0):
self.x = float(x)
self.y = float(y)

def __add__(self, val):
return Point( self[0] + val[0], self[1] + val[1] )

def __sub__(self,val):
return Point( self[0] - val[0], self[1] - val[1] )

def __iadd__(self, val):
self.x = val[0] + self.x
self.y = val[1] + self.y
return self

def __isub__(self, val):
self.x = self.x - val[0]
self.y = self.y - val[1]
return self

def __div__(self, val):
return Point( self[0] / val, self[1] / val )

def __mul__(self, val):
return Point( self[0] * val, self[1] * val )

def __idiv__(self, val):
self[0] = self[0] / val
self[1] = self[1] / val
return self

def __imul__(self, val):
self[0] = self[0] * val
self[1] = self[1] * val
return self

def __getitem__(self, key):
if( key == 0):
return self.x
elif( key == 1):
return self.y
else:
raise Exception("Invalid key to Point")

def __setitem__(self, key, value):
if( key == 0):
self.x = value
elif( key == 1):
self.y = value
else:
raise Exception("Invalid key to Point")

def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
Point = Vector

def DistanceSqrd( point1, point2 ):
'Returns the distance between two points squared. Marginally faster than Distance()'
return ( (point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)
def Distance( point1, point2 ):
'Returns the distance between two points'
return math.sqrt( DistanceSqrd(point1,point2) )
def LengthSqrd( vec ):
'Returns the length of a vector sqaured. Faster than Length(), but only marginally'
return vec[0]**2 + vec[1]**2
def Length( vec ):
'Returns the length of a vector'
return math.sqrt( LengthSqrd(vec) )
def Normalize( vec ):
'Returns a new vector that has the same direction as vec, but has a length of one.'
if( vec[0] == 0. and vec[1] == 0. ):
return Vector(0.,0.)
return vec / Length(vec)
def Dot( a,b ):
'Computes the dot product of a and b'
return a[0]*b[0] + a[1]*b[1]
def ProjectOnto( w,v ):
'Projects w onto v.'
return v * Dot(w,v) / LengthSqrd(v)



In addition, it seems that you can use NumPy for vector work, but I can't find much good documentation on how to do that. NumPy would require installation by end-users, but it's darn near standard for Python and if you're just playing around, that might not be an issue.

Hope that helps.
0

Share this post


Link to post
Share on other sites
What kind of vector class are you looking for? A mathematical vector class for doing orientations and dot products, etc. or a container vector class for storing stuff in?
0

Share this post


Link to post
Share on other sites
Sorry -- that could have been a little more clear. I'm looking for a mathematical vector class, not a container. I think I would want to be familiar with pretty much one of the most basic features of the language before I go about writing programs in it :)
0

Share this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
Sorry -- that could have been a little more clear. I'm looking for a mathematical vector class, not a container. I think I would want to be familiar with pretty much one of the most basic features of the language before I go about writing programs in it :)


One of the built-in Python types is 'complex', and complex numbers are (2D) points (or vectors).
0

Share this post


Link to post
Share on other sites
In almost all cases which make use of vector math, the array type in the numpy module is appropriate. All of the appropriate functions are implemented in optimized C, and the types are closely compatible with python lists.

Some 'common' operations on numpy arrays that you would expect on a custom 2 or 3 dimensional vector class aren't present (dot product), but are very easy to reproduce.

0

Share this post


Link to post
Share on other sites
I just found pyeuclid. "Vector, matrix and quaternion classes for use with 2D and 3D games and graphics applications".
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0