Moving an Enemy object in SDL
Nov 9, 2017 at 1:17am UTC
Hello everyone!
I'm attempting to move an enemy object towards a target (player). I'm using Vectors, and the enemy will move towards the object, but for some reason its bouncing between moving right and down, or left and up, or random other things.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// follow code, should take in a target.
float targetXPos{ 0.0f };
float targetYPos{ 0.0f };
int speed = 25;
using namespace std;
//GetPos
mFollowTarget->SetToTargetPos(targetXPos, targetYPos);
//aquire target position and normalize it.
MyMath::Float2 dir(targetXPos - mPos.x, targetYPos - mPos.y);
dir.x = dir.x / sqrt( pow(dir.x,2) + pow(dir.y,2));
dir.y = dir.y / sqrt( pow(dir.x,2) + pow(dir.y,2));
MyMath::Float2 vel = dir * speed * gDeltaTime;
//movement code.
if (vel.x > 0) {
//move left animation
if (vel.x > MyMath::Abs(vel.y)) {
cout << "moving left" << endl;
moveLeftTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveLeftTimer % ANIM_LEFT_COUNT;
mSpriteClipIndex = animLeftIndices[index];
lastMoveIndex = animLeftIndices[0];
}
//move down animation
else if (vel.y >= 0) {
cout << "moving down" << endl;
moveDownTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveDownTimer % ANIM_DOWN_COUNT;
mSpriteClipIndex = animDownIndices[index];
lastMoveIndex = animDownIndices[0];
}
//move up animation
else {
cout << "moving up" << endl;
moveUpTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveUpTimer % ANIM_UP_COUNT;
mSpriteClipIndex = animUpIndices[index];
lastMoveIndex = animUpIndices[0];
}
}
else {
//move right animation
if (vel.x < MyMath::Abs(vel.y)) {
cout << "moving right" << endl;
moveRightTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveRightTimer % ANIM_RIGHT_COUNT;
mSpriteClipIndex = animRightIndices[index];
lastMoveIndex = animRightIndices[0];
}
//move down animation
else if (vel.y >= 0) {
cout << "moving down" << endl;
moveDownTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveDownTimer % ANIM_DOWN_COUNT;
mSpriteClipIndex = animDownIndices[index];
lastMoveIndex = animDownIndices[0];
}
//move up animation
else {
cout << "moving up" << endl;
moveUpTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveUpTimer % ANIM_UP_COUNT;
mSpriteClipIndex = animUpIndices[index];
lastMoveIndex = animUpIndices[0];
}
}
//follow player code
if (targetXPos <= mPos.x) {
mPos.x -= speed * gDeltaTime;
}
if (targetYPos <= mPos.y) {
mPos.y -= speed * gDeltaTime;
}
if (targetXPos >= mPos.x) {
mPos.x += speed * gDeltaTime;
}
if (targetYPos >= mPos.y) {
mPos.y += speed * gDeltaTime;
}
}
Last edited on Nov 9, 2017 at 9:57pm UTC
Nov 9, 2017 at 5:52pm UTC
Can you use code tags, and perhaps a full program that reproduces the same problem?
Nov 9, 2017 at 10:00pm UTC
i have corrected the issue, but at the same time caused another :-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void Guard::Follow() {
//GetPos
mFollowTarget->SetToTargetPos(targetXPos, targetYPos);
MyMath::Float2 targetPos(targetXPos, targetYPos);
//aquire target position and normalize it.
//MyMath::Float2 dir(targetXPos - mPos.x, targetYPos - mPos.y);
//dir.x = dir.x / (sqrt( pow(dir.x,2) + pow(dir.y,2)));
//dir.y = dir.y / (sqrt( pow(dir.x,2) + pow(dir.y,2)));
MyMath::Float2 dir = MyMath::Normalize(targetPos - mPos);
MyMath::Float2 vel = dir * speed * gDeltaTime;
float horizVel = MyMath::Abs(vel.x);
float vertVel = MyMath::Abs(vel.y);
//movement and sprite animations
if (horizVel > vertVel) {
if (vel.x > 0) {
moveRightTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveRightTimer % ANIM_RIGHT_COUNT;
mSpriteClipIndex = animRightIndices[index];
lastMoveIndex = animRightIndices[0];
}
else {
moveLeftTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveLeftTimer % ANIM_LEFT_COUNT;
mSpriteClipIndex = animLeftIndices[index];
lastMoveIndex = animLeftIndices[0];
}
}
else if (vertVel > horizVel) {
if (vel.y > 0) {
moveDownTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveDownTimer % ANIM_DOWN_COUNT;
mSpriteClipIndex = animDownIndices[index];
lastMoveIndex = animDownIndices[0];
}
else {
moveUpTimer += animMoveSpeed * gDeltaTime;
int index = (int )moveUpTimer % ANIM_UP_COUNT;
mSpriteClipIndex = animUpIndices[index];
lastMoveIndex = animUpIndices[0];
}
}
mPos += vel;
}
the above code is the fixed problem, for the most part.
an issue im running into now, is in mymath.h i have in the mymath namespace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
struct Float2 {
Float2() {}
Float2(float x, float y) : x(x), y(y) {}
float x{ 0.f };
float y{ 0.f };
Float2 operator *(float right) {
Float2 value(x *right, y * right);
return value;
}
Float2 operator /(float right) {
Float2 value(x * right, y * right);
return value;
}
void operator +=(Float2 right) {
x += right.x;
y += right.y;
}
Float2 operator +(Float2 right) {
Float2 result(x - right.x, y + right.y);
return result;
}
Float2 operator -(Float2 right) {
Float2 result(x - right.x, y - right.y);
return result;
}
};
float Magnitude(Float2 value) {
return sqrt( pow(value.x, 2) + pow(value.y, 2));
}
Float2 Normalize(Float2 value) {
return value / Magnitude(value);
}
the bottom two, magnitude and normalize are producing a linker error 2005 for virtually every class i have in the program
Nov 9, 2017 at 10:02pm UTC
Move the function definitions to a source file, or declare them as inline.
1 2 3
inline float Magnitude(Float2 value) {
return sqrt( pow(value.x, 2) + pow(value.y, 2));
}
Last edited on Nov 9, 2017 at 10:02pm UTC
Topic archived. No new replies allowed.