float MAX_SPEED = 1.0f;
float RADIUS = 3.0f;
float Distance (vector3 &v1, vector3 &v2)
{
vector3 v;
v.x = v2.x - v1.x;
v.y = v2.y - v1.y;
v.z = v2.z - v1.z;
return sqrtf (v.x * v.x + v.y * v.y + v.z * v.z);
}
void Normalize (vector3 &v)
{
float m = sqrtf (v.x * v.x + v.y * v.y + v.z * v.z);
if (m)
{
m = 1.0f / m;
v.x *= m;
v.y *= m;
v.z *= m;
}
}
void AI_Update (vector3 &Target, float dTime)
{
float D = Distance (m_Position, Target);
if (D <= RADIUS)
return;
vector3 Dir;
Dir.x = Target.x - m_Position.x;
Dir.y = Target.y - m_Position.y;
Dir.z = Target.z - m_Position.z;
Normalize (Dir);
Dir.x *= -1;
Dir.y *= -1;
Dir.z *= -1;
float Length = RADIUS - D;
float MaxSpeed = MAX_SPEED * dTime;
if (Length > MaxSpeed)
Length = MaxSpeed;
m_Position.x += Dir.x * Length;
m_Position.y += Dir.y * Length;
m_Position.z += Dir.z * Length;
printf ("%f %f %f\n", m_Position.x, m_Position.y, m_Position.z);
}
🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Simple: Following a point
Hi
Okay, i''m very new to AI and 3D in general, but i was playing around with some code which makes one point move towards another point until it is within a certain radius (yeah, i know it''s dumb). There''s no other obsticles or anything. All it does is move towards a dot on the screen (which i control).
The problems are that
1) it seems to ignore all notions of time (it just jumps to the radius, regardless of the timestep)
2) sometimes it will be just outside the radius and wont move itself inside.
i know when #2 occurs due to the printf statement (it just keeps on printing the same 3 values over and over again until i move).
even if i increase my dot''s movement to 10 units per frame (the ai dot can do 1 unit per second!) it still jumps to the closest point on the radius.
any ideas?
thanks
try replacing:
-------------
Dir.x *= -1;
Dir.y *= -1;
Dir.z *= -1;
float Length = RADIUS - D;
-------------
By
-------------
float Length = D;
-------------
the problem is that as D is allways bigger than RADIUS, Length is negative, and thus the comparison that you perform after that:
if (Length > MaxSpeed)
Length = MaxSpeed;
is allways false.
-------------
Dir.x *= -1;
Dir.y *= -1;
Dir.z *= -1;
float Length = RADIUS - D;
-------------
By
-------------
float Length = D;
-------------
the problem is that as D is allways bigger than RADIUS, Length is negative, and thus the comparison that you perform after that:
if (Length > MaxSpeed)
Length = MaxSpeed;
is allways false.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement