Skip to main content
GameDev.net gamedev.net
🔒 Locked

simple vector physic engine

Started by DMINATOR Jan 13, 2005 at 5:29 AM 4 replies 1.3k views
Original Post
DMINATOR
DMINATOR
well, I am working on my game right now, and I am a little confused and uncertain, how to make my engine, simple and more logical. The game is 2d top down. So all i need is simple 2d vectors to assign direction to move. I have 2 typed of vectors - dynamical ( that change it values by resisatances ( such as friction)) and static (it is used while user keeps button pressed).Also I have a rotation vector (statical and dynamical also) Well it was pretty simple at beginning, but now when I rotate the object, it becomes pretty impossible to keep up with static vectors. For example I pressed a button (to move player forward) It adds static vector, and the player moves. Now when I rotate player with other key, also player direction should be changed to the new one, that's fine I change it, but when the forward button unpressed some vector direction is still present. Well what suggestions do you have ? Maybe you can suggest a better system ? Tnx in advance
oliii
oliii
I would say, don;t bother with sub-classing vectors. use a single vector class.
Everything is better with Metal.
DMINATOR
DMINATOR
I can hardly think how can I use that.
Gorax
Gorax
You could always use the same approach as what's used in ODE. Use a base type (in this case, it's your Vector class), and then keep a pointer to some extra data. Let that be your extension, rather than actually changing the type of vector.
oliii
oliii
Quote:
Original post by DMINATOR
I can hardly think how can I use that.


I don;t get it... Vectors are the simplest construct you can use in game physics, and should be kept that way as much as possible. Here is an example.

#ifndef OLI_VECTOR_H#define OLI_VECTOR_H/*------------------------------------------------------------------File: Vector.hStarted: 09/01/2004 22:20:40  $Header: $$Revision: $$Locker: $$Date: $  Author: Olivier renault------------------------------------------------------------------Module: Description: ------------------------------------------------------------------$History: $------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <gl/glut.h>typedef unsigned char   u_char;typedef unsigned short  u_short;typedef unsigned int    u_int;typedef unsigned long   u_long;#define for if(0); else forinline float sign(float x){	return (x < 0.0f)? -1.0f : 1.0f;}inline float frand(float x=1.0f){	return (rand() / (float) RAND_MAX) * x;}inline swapf(float& a, float& b){	float c = a;	a = b;	b = c;}inline float clampf(float x, float min, float max){	return (x < min)? min : (x > max)? max : x;}inline float wrapf(float x, float min, float max){	return (x < min)? (x - min) + max : (x > max)? (x - max) + min : x;}inline float Pi(){    const float pi = atan(1.0f) * 4.0f;	return pi;}inline float TwoPi(){	const float two_pi = 2.0f * Pi();	return two_pi;}inline float RadiansToDegrees(float rad){	const float k = 180.0f / Pi();	return rad * k;}inline float DegreesToRadians(float deg){	const float k = Pi() / 180.0f;	return deg * k;}//===========================================================================// VECTORS//===========================================================================class Matrix;class Vector{public:	float x,y;	static const Vector& Blank() { static const Vector V(0, 0); return V; }public:	inline Vector(void)	{}	inline Vector(float Ix,float Iy)	: x(Ix)	, y(Iy)	{}	inline Vector &operator /=(const float Scalar)	{ x /= Scalar; y /= Scalar;		return *this; }	inline Vector &operator *=(const float Scalar)	{ x *= Scalar; y *= Scalar;		return *this; }		inline Vector &operator +=(const Vector &Other) { x += Other.x;	y += Other.y;	return *this; }	inline Vector &operator -=(const Vector &Other) { x -= Other.x;	y -= Other.y;	return *this;	}	inline float operator ^ (const Vector &V)	const	{	return (x * V.y) - (y * V.x); } // cross product	inline float operator * (const Vector &V)	const	{	return (x*V.x) + (y*V.y); } // dot product	inline Vector operator * (float  s)			const	{	return Vector(x*s, y*s); }		inline Vector operator / (float  s)			const	{	return Vector(x/s, y/s); }		inline Vector operator + (const Vector &V)	const	{	return Vector(x+V.x, y+V.y); }			inline Vector operator - (const Vector &V)	const	{	return Vector(x-V.x, y-V.y); }	friend Vector operator * (float k, const Vector& V) {	return Vector(V.x*k, V.y*k); }	Vector operator * (const Matrix& M) const;		Vector operator ^ (const Matrix& M) const;		Vector& operator *=(const Matrix& M);		Vector& operator ^=(const Matrix& M);	inline Vector operator -(void) const { return Vector(-x, -y); }		inline float Length(void) const { return (float) sqrt(x*x + y*y); }	float Normalise(void) 	{			float fLength = Length();					if (fLength == 0.0f) 			return 0.0f; 				(*this) *= (1.0f / fLength); 			return fLength;		}	Vector Direction(void) const	{		Vector Temp(*this);		Temp.Normalise();		return Temp;	}	float Angle(const Vector& xE)	{		float dot = (*this) * xE;		float cross = (*this) ^ xE;				// angle between segments		float angle = (float) atan2(cross, dot);		return angle;	}	Vector& Rotate(float angle)	{		float tx = x;		x =  x * cos(angle) - y * sin(angle);		y = tx * sin(angle) + y * cos(angle);		return *this;	}	Vector& Rotate(const Vector& xCentre, float fAngle)	{		Vector D = *this - xCentre;		D.Rotate(fAngle);		*this = xCentre + D;		return *this;	}	void Clamp(const Vector& min, const Vector& max)	{		x = (x < min.x)? min.x : (x > max.x)? max.x : x;		y = (y < min.y)? min.y : (y > max.y)? max.y : y;	}	void Randomise(const Vector& xMin, const Vector& xMax)	{		x = frand(xMax.x - xMin.x) + xMin.x;		y = frand(xMax.y - xMin.y) + xMin.y;	}};#endif OLI_VECTOR_H
Everything is better with Metal.
DMINATOR
DMINATOR
Ok tnx the code is really handy, but let me show you what I am talking about:

Here is my object structure:
Quote:

posx,posy - player position
vstat - statical vector
vdyn - dynamical vector
rot - rotation angle

//and here is the way postion changes as game updates

posx += vstat.x + vdyn.x;
posy += vstat.y + vdyn.y;


Here are the keys

Quote:

//KEY_DOWN - player moves forward
// I assign the vector dirrection

vstat = (+2,0);

//KEY_UP - player stops moving
//I remove the vector
vstat = (-2,0);


Everything is fine for a moment, but when I add rotation to the player, I need to update the statical vector to the new angle so the player would change it's trajectory , but statical vector may consist of vectors that don't need to be rotated .

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.