Simple Physics/Math help with C++ Game
I'm currently making a 2D game and I'm not sure about how to do a few things on it. The main problem is with the enemies. They need to be spawned at a random location, ok I can handle that easily enough, I also need to set their direction vector to something random, I created an _vector2 class with int x and y values, so I can do that, but when it actually comes to updating it's location, I get lost. I don't really know how to make it move. Can someone help me here? I would like to update it by calculating displacment in relation to time. Info that might help? The enemies position is simply changed with a call to Enemy.SetPosition(int xPosition, yPosition);
Can someone help me with getting my randomly placed enemy to move in a random location? I would appreciate all help. Any questions regarding the post please ask.
When you say random direction, do u mean random direction per frame or random direction per enemy spawn?
either way its a similar process.
I would go about it by doing something like this:
NOTE:1) you might need to change your function for the right parameters
2) You can get time calculated from your game loop
Here is an article on game loops :http://dewitters.koonsolo.com/gameloop.html
Hope this helped
either way its a similar process.
I would go about it by doing something like this:
void update(float changeTime){ enemyDir = makeEnemyDir(); //just update it like this enemyPos.x += enemyDir.x * changeTime; //s = v * t enemyPos.y += enemyDir.y * changeTime;}
NOTE:1) you might need to change your function for the right parameters
2) You can get time calculated from your game loop
Here is an article on game loops :http://dewitters.koonsolo.com/gameloop.html
Hope this helped
It's a random direction per spawn.
A few things have occured to me, I can't really just make a random coordinate and just move towards it, because once it reaches that location won't it just stop? I want it to continue on then I'll get it to bounce off the side. And how would I get changeTime?
Here's a little more info, if someone can help me, tell me if I'm doing it
right so far.
[Edited by - Googol PL3X on September 17, 2008 7:23:11 AM]
A few things have occured to me, I can't really just make a random coordinate and just move towards it, because once it reaches that location won't it just stop? I want it to continue on then I'll get it to bounce off the side. And how would I get changeTime?
Here's a little more info, if someone can help me, tell me if I'm doing it
right so far.
class vector2{public: vector2() : x(0.0), y(0.0) {}; vector2(int a_X, int a_Y) : x(a_X), y(a_Y) {}; int getx() { return x; } int gety() { return y; }private: int x, y; };class Enemy{ public: Enemy(); ~Enemy(); void Update(); wsp::Sprite *get_Sprite(); void on_collision() { }; private: wsp::Sprite *_enemy; vector2 m_Origin; vector2 m_Direction; };
[Edited by - Googol PL3X on September 17, 2008 7:23:11 AM]
Firecore, after I've done some math on paper, I don't think that the forumla you gave would work in the given situation.
Say for example:
posx = 5
posy = 5
dirx = 10
diry = 5
The following should only move it across, but using your formula, lets assume changeTime = 0.0001 seconds, the following results would occur
posx = posx + (10 * 0.0001)
posx = 5 + 0.001
pox = 5.001
That's ok, but when we consider the y axis
posy = posy + (5 * 0.0001)
posy = 5 + 0.0005
posy = 5.0005
posy shouldn't change at all in the given scenario as they are equal to begin with, your given formula seemed to only work on 45 degree angles. Please correct me if I'm wrong.
Say for example:
posx = 5
posy = 5
dirx = 10
diry = 5
The following should only move it across, but using your formula, lets assume changeTime = 0.0001 seconds, the following results would occur
posx = posx + (10 * 0.0001)
posx = 5 + 0.001
pox = 5.001
That's ok, but when we consider the y axis
posy = posy + (5 * 0.0001)
posy = 5 + 0.0005
posy = 5.0005
posy shouldn't change at all in the given scenario as they are equal to begin with, your given formula seemed to only work on 45 degree angles. Please correct me if I'm wrong.
void update(float delta_seconds){ vector2 targetLoc(newX, newY); // where is enemy going float enemySpeed = 1.2f; // enemy moves 1.2 units a second vector2 enemyDir = normalize(targetLoc - enemyPos); // this is a direction (remember ->> normalize(to - from)) vector2 enemyVelocity = enemyDir * enemySpeed; // this is a velocity //just update it like this enemyPos += (enemyVelocity * delta_seconds); //s = v * t}}
:: changed float3 to vector2
[Edited by - WOsborn on September 18, 2008 6:54:48 AM]
Hey, what's float3? Is that supposed to by my vector?
Ohk, another update, I think I have it this time, this is my vector class:
and I use the update function similar to what you did
and enemyPosition.x and .y are passed to this->SetPosition(x, y);
[Edited by - Googol PL3X on September 17, 2008 11:01:45 PM]
Ohk, another update, I think I have it this time, this is my vector class:
#define NORMALIZE(A) {float l=1/sqrtf(A.x*A.x+A.y*A.y);A.x*=l;A.y*=l;}class vector2{public: vector2(float newx, float newy) { x = newx; y = newy; } friend vector2 operator - ( vector2 v1, vector2 v2 ) { return vector2( v1.x - v2.x, v1.y - v2.y ); } friend vector2 operator * ( vector2 v1, vector2 v2 ) { return vector2( v1.x * v2.x, v1.y * v2.y ); } friend vector2 operator * ( vector2 v, float f ) { return vector2( v.x * f, v.y * f ); } friend vector2 operator += (vector2 v1, vector2, v2) { return vector2( v1.x + v2.x, v1.y + v2.y ); } //enemyPosition += (enemyVelocity * delta_seconds); //s = v * t float x, y;};
and I use the update function similar to what you did
void Enemy::Update(float delta_seconds){ /*vector2 targetLocation = (newX, newY); // where is enemy going float enemySpeed = 1.2; // enemy moves 1.2 units a second vector2 enemyDirection = NORMALIZE (targetLocation - enemyPosition); // this is a direction (remember ->> normalize(to - from)) vector2 enemyVelocity = enemyDirection * enemySpeed; // this is a velocity enemyPosition += (enemyVelocity * delta_seconds); //s = v * t*/}
and enemyPosition.x and .y are passed to this->SetPosition(x, y);
[Edited by - Googol PL3X on September 17, 2008 11:01:45 PM]
Quote:Original post by Googol PL3X
Firecore, after I've done some math on paper, I don't think that the forumla you gave would work in the given situation.
Say for example:
posx = 5
posy = 5
dirx = 10
diry = 5
The following should only move it across, but using your formula, lets assume changeTime = 0.0001 seconds, the following results would occur
posx = posx + (10 * 0.0001)
posx = 5 + 0.001
pox = 5.001
That's ok, but when we consider the y axis
posy = posy + (5 * 0.0001)
posy = 5 + 0.0005
posy = 5.0005
posy shouldn't change at all in the given scenario as they are equal to begin with, your given formula seemed to only work on 45 degree angles. Please correct me if I'm wrong.
It works because we assume that the x and y values are vector components.
look here : http://www.gamedev.net/reference/programming/features/vecmatprimer/
Although that article talks in 3D, the concepts remain the same.
float3 is you vector, but i feel it should be float2 because u are working in 2D, but its just psudocode. So dont worry.
And if you want to check your vector class, there was a similar article here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=302771
Ohk, done some reading! Thanks for everything, but why floatn, it's not a floating point value of any type.. it's a vector, or is the float2 supposed to refer to the 2 float x and y values?
Quote:Original post by Googol PL3X
Ohk, done some reading! Thanks for everything, but why floatn, it's not a floating point value of any type.. it's a vector, or is the float2 supposed to refer to the 2 float x and y values?
Yeh. i assume the float2 refers to the fact that there are 2 float values, x and y.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement