Jump to content


Photo

Vector2D - vector engine to minimize coding


  • Please log in to reply
6 replies to this topic

#1 deluksic

deluksic

    GMC Member

  • GMC Member
  • 54 posts
  • Version:GM8

Posted 25 June 2012 - 02:22 PM

Introduction

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:


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.

  • 1

#2 swang99

swang99

    GMC Member

  • GMC Member
  • 40 posts
  • Version:GM8

Posted 27 June 2012 - 09:34 AM

Introduction

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:


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!

Nice! Might try it out sometimes *^^*" NE~
  • 0

#3 Qon

Qon

    GMC Member

  • GMC Member
  • 85 posts

Posted 05 July 2012 - 01:48 AM

Gaaaaahhh!! Vectors in gamemaker, my 3D engine explodes because of all the vector additions. And I don't want to use scripts for those because GML isn't fast enough for that!
I do use things like these for scripts that do complex stuff, but simple vector math is used so often that it would slow my engine down to much. In a 2D game you don't really have the same speed limitations, so this would be very very useful for 2D game makers. And I only do 3D.

Swang why quote the whole post if you are just going to respon with "nice"? .....
  • 0

#4 deluksic

deluksic

    GMC Member

  • GMC Member
  • 54 posts
  • Version:GM8

Posted 07 July 2012 - 10:09 PM

Gaaaaahhh!! Vectors in gamemaker, my 3D engine explodes because of all the vector additions. And I don't want to use scripts for those because GML isn't fast enough for that!
I do use things like these for scripts that do complex stuff, but simple vector math is used so often that it would slow my engine down to much. In a 2D game you don't really have the same speed limitations, so this would be very very useful for 2D game makers. And I only do 3D.

Swang why quote the whole post if you are just going to respon with "nice"? .....


yes i know it is slow.. Maybe they could make something like this to be in default GML script set, it would be somewhat faster and it would be very useful, as well for 3D.

Swang really you didn't have to quote all that.. but thanks ;)
  • 0

#5 Master Xilo

Master Xilo

    GMC Member

  • GMC Member
  • 379 posts
  • Version:GM8

Posted 29 July 2012 - 12:05 AM

Nice. You could add creation via polar coordinates (angle and radius), complex-number style vector multiplication (which geometrically seen applies a rotation-dilation of the angle and size of one vector to the other) and through that, rotation by an arbitrary amount of degrees.
  • 0

#6 gamemakerfun300

gamemakerfun300

    GMC Member

  • GMC Member
  • 42 posts
  • Version:GM:Studio

Posted 11 August 2012 - 11:20 PM

This could be useful. I will have to try it out sometime.
  • 0

#7 Primoz128

Primoz128

    GMC Member

  • GMC Member
  • 278 posts
  • Version:GM:Studio

Posted 24 November 2012 - 01:30 PM

Sounds great.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users