I've been programming in game maker for last few years, and I often find myself writing x and y sets of variables for every position, speed, force vector etc.
So I thought, why not make a universal engine for these?
I've been also programming in XNA game studio, where you have integrated vector system, that makes your code look simpler and cleaner
for example:
//instead of speedx += accelerationx; speedy += accelerationy; //you write //v2d_add(vector1, vector2, destination vector) v2d_add(speed, acceleration, speed)
How engine works
The vectors in the engine are something like surfaces. You can create them by using v2d_create(x,y) function which returns the id of the vector, and later you use that id to make all mathematical operations on them (also included functions). You can also delete a vector by using the v2d_free(id) which doesn't actually delete it, but makes it available to overwrite.
This is an example of how you would use it:
//create event position = v2d_create(0, 0); speed = v2d_create(0, 0); acceleration = v2d_create(0, 0); //step event v2d_add(speed, acceleration, speed); v2d_add(position, speed, position); //draw event draw_sprite_v2d(sprite_index, image_index, position);
Current function list
- v2d_init() - initializes the vector engine
To access each component of the vector, just use:
- v2d_x[id];
- v2d_y[id];
Managing vectors
- v2d_create(x,y);
- v2d_free(id);
- v2d_copy(source, destination);
Setting vectors
- v2d_set_xy(id, x, y);
- v2d_set_lendir(id, len, dir);
- v2d_set_length(id, len);
- v2d_set_point(id, x1, y1, x2, y2);
Mathematical functions
- v2d_add(id1, id2, destination);
- v2d_subtract(id1, id2, destination);
- v2d_dot(id1, id2);
- v2d_cross(id1, id2);
- v2d_angle2(id1, id2);
- v2d_project(id1, id2, destination);
- v2d_distance(id1, id2);
- v2d_multiply(id, amount);
- v2d_normalize(id);
- v2d_rotate90(id, clockwise?)
- v2d_length(id);
Drawing functions (more to come)
- draw_sprite_v2d(sprite_index, image_index, id);
- draw_sprite_ext_v2d(...)
- draw_vector(id, x, y);
- draw_vector_v2d(id, position vector id);
- draw_arc_v2d(id1, id2, x, y, radius);
DOWNLOAD:
- Vector2D.rar (16.4 KB)
- Vector2D.gmk (17.7 KB)
The download contains scripts and examples of usage. When you run the game, adjust vectors by 'left mouse button' and go trough rooms by 'Enter'
New function suggestions are always welcome!
Edited by deluksic, 25 June 2012 - 02:53 PM.











