• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #ActualAardvajk

Posted 10 January 2013 - 09:26 AM

Here's a very basic 2D vector class:


#ifndef VEC2_H

#define VEC2_H

#include

class Vec2

{

public:

Vec2(){ }

Vec2(float x, float y) : x(x), y(y) { }

Vec2 &operator+=(const Vec2 &v){ x += v.x; y += v.y; return *this; }

Vec2 &operator-=(const Vec2 &v){ x -= v.x; y -= v.y; return *this; }

Vec2 &operator*=(float v){ x *= v; y *= v; return *this; }

Vec2 operator+(const Vec2 &v) const { Vec2 r = *this; r += v; return r; }

Vec2 operator-(const Vec2 &v) const { Vec2 r = *this; r -= v; return r; }

Vec2 operator-() const { return *this * -1.0f; }

Vec2 operator*(float v) const { Vec2 r = *this; r *= v; return r; }

float dot(const Vec2 &v) const { return (x * v.x) + (y * v.y); }

float length() const { return sqrt(dot(*this)); }

Vec2 normalised() const { float n = length(); return Vec2(x / n, y / n); }

float x, y;

};

inline Vec2 operator*(float f, const Vec2 &v){ Vec2 r = v; r *= f; return r; }

#endif // VEC2_H



Now it is simple:


void f()

{

Vec2 pos(50, 40);

Vec2 target(200, 300);

Vec3 dir = target - pos;

dir = dir.normalised();

while((target - pos).length() < epsilon)

{

pos += dir * speed;

}

pos = target;

}



A vector class with overloads is a great idea when doing this kind of stuff.

Post editor ate my spaces. Sorry. The editor is still glitchy as hell on this site.

### #2Aardvajk

Posted 10 January 2013 - 09:25 AM

Here's a very basic 2D vector class:

#ifndef VEC2_H
#define VEC2_H

#include

class Vec2
{
public:
Vec2(){ }
Vec2(float x, float y) : x(x), y(y) { }

Vec2 &operator+=(const Vec2 &v){ x += v.x; y += v.y; return *this; }
Vec2 &operator-=(const Vec2 &v){ x -= v.x; y -= v.y; return *this; }

Vec2 &operator*=(float v){ x *= v; y *= v; return *this; }

Vec2 operator+(const Vec2 &v) const { Vec2 r = *this; r += v; return r; }
Vec2 operator-(const Vec2 &v) const { Vec2 r = *this; r -= v; return r; }

Vec2 operator-() const { return *this * -1.0f; }

Vec2 operator*(float v) const { Vec2 r = *this; r *= v; return r; }

float dot(const Vec2 &v) const { return (x * v.x) + (y * v.y); }

float length() const { return sqrt(dot(*this)); }

Vec2 normalised() const { float n = length(); return Vec2(x / n, y / n); }

float x, y;
};

inline Vec2 operator*(float f, const Vec2 &v){ Vec2 r = v; r *= f; return r; }

#endif // VEC2_H


Now it is simple:

void f()
{
Vec2 pos(50, 40);
Vec2 target(200, 300);

Vec3 dir = target - pos;
dir = dir.normalised();

while((target - pos).length() < epsilon)
{
pos += dir * speed;
}

pos = target;
}


A vector class with overloads is a great idea when doing this kind of stuff.

### #1Aardvajk

Posted 10 January 2013 - 09:25 AM

Here's a very basic 2D vector class:<br />&nbsp;<br />
<br />#ifndef VEC2_H<br />#define VEC2_H<br />&nbsp;<br />#include &lt;math.h&gt;<br />&nbsp;<br />class Vec2<br />{<br />public:<br />&nbsp; &nbsp; Vec2(){ }<br />&nbsp; &nbsp; Vec2(float x, float y) : x(x), y(y) { }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 &amp;operator+=(const Vec2 &amp;v){ x += v.x; y += v.y; return *this; }<br />&nbsp; &nbsp; Vec2 &amp;operator-=(const Vec2 &amp;v){ x -= v.x; y -= v.y; return *this; }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 &amp;operator*=(float v){ x *= v; y *= v; return *this; }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 operator+(const Vec2 &amp;v) const { Vec2 r = *this; r += v; return r; }<br />&nbsp; &nbsp; Vec2 operator-(const Vec2 &amp;v) const { Vec2 r = *this; r -= v; return r; }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 operator-() const { return *this * -1.0f; }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 operator*(float v) const { Vec2 r = *this; r *= v; return r; }<br />&nbsp;<br />&nbsp; &nbsp; float dot(const Vec2 &amp;v) const { return (x * v.x) + (y * v.y); }<br />&nbsp;<br />&nbsp; &nbsp; float length() const { return sqrt(dot(*this)); }<br />&nbsp;<br />&nbsp; &nbsp; Vec2 normalised() const { float n = length(); return Vec2(x / n, y / n); }<br />&nbsp;<br />&nbsp; &nbsp; float x, y;<br />};<br />&nbsp;<br />inline Vec2 operator*(float f, const Vec2 &amp;v){ Vec2 r = v; r *= f; return r; }<br />&nbsp;<br />#endif // VEC2_H<br />
<br />&nbsp;<br />Now it is simple:<br />&nbsp;<br />
<br />void f()<br />{<br />Vec2 pos(50, 40);<br />&nbsp; &nbsp; Vec2 target(200, 300);<br /><br />Vec3 dir = target - pos;<br />dir = dir.normalised();<br /><br />while((target - pos).length() &lt; epsilon)<br />{<br />pos += dir * speed;<br />}<br /><br />pos = target;<br />}<br />
<br /><br />A vector class with overloads is a great idea when doing this kind of stuff.

PARTNERS