Can anyone convert 4 lines of C++ for me?

Hi guys,

I am just looking into some ai behaviours and am struggling to get my head around this at the moment. Want to get this simple Seek behaviour to work but can only find a code sample in c++. Anyone able to change it into Java so i can understand it better?

Vector2D SteeringBehaviors::Seek(Vector2D TargetPos)
{
Vector2D DesiredVelocity = Vec2DNormalize(TargetPos m_pVehicle->Pos())
* m_pVehicle->MaxSpeed();

return (DesiredVelocity m_pVehicle->Velocity());
}

First the desired velocity is calculated. This is the velocity the agent would need to reach the target position in an ideal world. It represents the vector from the agent to the target, scaled to be the length of the maximum possible speed of the agent.

The steering force returned by this method is the force required, which when added to the agent’s current velocity vector gives the desired velocity. To achieve this you simply subtract the agent’s current velocity from the desired velocity. See Figure 3.2.

Thanks in advance if anyone can help! Or better yet, if anyone knows any Java Artificial intelligence sites that would be so helpful :slight_smile:

In Java you don’t have operator overloading, so you’ll have to write your own methods to handle vector-math.

Vec add(Vec a, Vec b) {
  Vec c = new Vec();
  c.x = a.x + b.x;
  c.y = a.y + b.y;
  return c;
}

I don’t see any other problems you’d have with that code. If this really troubles you this much, you might want to study Java a bit more, before you dive into AI

There is absolutely nothing hard in the C++, nothing really requires a ‘math-lib’ (only subtraction, multiplication & normalize), so that’s why I suggested you looked more into the basics.

Oh well.

Hm… you removed you post I replied to. ::slight_smile:

Yea, its just the vector normalization part i didnt understand what was happening in there.

http://www.google.com/search?q=normalize+vector