   moving around target/player
  Hi guys i have 2 methods to move my sprite, moveX and moveZ. What i want to do is move my enemy sprites around a target using moveX and moveZ which just increments the x or z positions i.e x += speed, z += speed. I am not sure if this is right but so far i have thisfloat x = enemy->GetPosX() - player->GetPosX();float z = enemy->GetPosZ() - player->GetPosZ();float angle = atan2(z, x);float distance = sqrt(x*x + z*z);enemy->MoveX((player->GetPosX() + cos(angle + deg) * distance));enemy->MoveZ((player->GetPosZ() + sin(angle + deg) * distance));enemy->CheckState(::WALK_TOWARDS);i don't think moveX and moveZ will get the right speed to go around target?The behaviour should be something like circling around the player?[Edited by - lodoss118 on May 13, 2010 10:53:34 AM]
  For circling the player, of course you want to consider the player as being at the center of a circle. The enemy then wants to be at some specific distance from the player (the radius), and they want to be at a specific angle. They'll keep increasing the angle so that they circle around the player. Assume the player is at (px, py) (I'm using X and Y since they're more traditional for 2D problems), the enemy is at (ex, ey), and the enemy wants to be at distance r and angle theta:rx = r * cos(theta); // Offset from player in Xry = r * sin(theta); // Offset from player in Y// Enemy's target position is (px + rx, py + ry). Do vector subtraction to get vector// from enemy to targetdx = px + rx - ex;dy = py + ry - dy;// (dx, dy) is the vector from the enemy's current position to their targetMake sense?On future update cycles, just change theta and the enemy will aim for a new position on the circle.   ;  Kinda but as i am not using vectors i was hoping someone would show me using the moveX and some calculation to do circling around   ;  At the end of my code snippet, I have dx and dy. If I add dx to the creature's X position, and add dy to the creature's Y position, then the creature will be where it needs to be. The "vector subtraction" I mentioned in my code is just the geometric name for finding the direction and distance from one point to another. I'm intentionally avoiding using your moveX and moveZ functions because the important thing is for you to understand the underlying geometry. You really need to have a decent grasp of geometry and basic trigonometry to write games with moving objects.   ;  Hi Dek the original code i posted worked fine if i just used SetPosX and SetPosZ which just assigns the x and z values. What i now did was this float rx = (player->GetPosX() + cos(angle + deg) * radius);float rz = (player->GetPosZ() + sin(angle + deg) * radius);float dx = enemy->GetPosX() - rx;float dz = enemy->GetPosZ() - rz;float mag = atan2(dz, dx);enemy->MoveX(-cos(mag)*2);enemy->MoveZ(-sin(mag)*2);now this is perfect, depending on the radius the enemy comes close to the player then once reached the radius, it circles the player :@   