1. Member
Join Date
Feb 2012
Posts
20
Rep Power
0

Math help

I'm new to JAVA, porting a processing sketch over for improvement. I recently decided to change my enemy AI, and make the enemies veer away from the player if they get close. I figured I could accomplish this by moving the enemies targetX and targetY over, depending on their angle to the player. Since I'm not yet familiar with the more advanced trigonometry, I need help with this. Attached is an image visualizing my problem. I need an algorithm to change point A to pint B.

If this helps, here is the current AI code:

Java Code:
```// distance from mouse to player
double distX = Main.myPlayer.getX() - x;
double distY = Main.myPlayer.getY() - y;

// get total distance as one number
double distTotal = Math.sqrt(distX * distX + distY * distY) + 1; // add
// 1 to fix / by 0 problem

// calculate how much to move
double moveDistanceX = turnRadius * distX / distTotal;
double moveDistanceY = turnRadius * distY / distTotal;

// increase current speed
velocityX += moveDistanceX;
velocityY += moveDistanceY;

// get total move distance
double totalMove = Math.sqrt(velocityX * velocityX + velocityY * velocityY);

// apply easing
velocityX = speed * velocityX / totalMove;
velocityY = speed * velocityY / totalMove;

// apply velocity
x += velocityX;
y += velocityY;

// apple angle
angle = -Math.atan2(velocityX, velocityY);```

2. Re: Math help

Where does the clear blue dotted line come from?

kind regards,

Jos

3. Member
Join Date
Feb 2012
Posts
20
Rep Power
0

Re: Math help

Only the Player and Enemy actually appear on screen. Everything else is just for visualization.

Blue line: Current Enemy path
Purple Line: Desired Enemy Path

4. Re: Math help

Originally Posted by stuffses
Blue line: Current Enemy path
Purple Line: Desired Enemy Path
Ah, ok, I understand now; shouldn't the enemy always have its 'nose' pointed towards the player then?

kind regards,

Jos

5. Member
Join Date
Feb 2012
Posts
20
Rep Power
0

Re: Math help

Originally Posted by JosAH
Ah, ok, I understand now; shouldn't the enemy always have its 'nose' pointed towards the player then?

kind regards,

Jos

The enemy attempts to chase the player, and its angle will always be the direction it is moving in. I just need a way to direct the Enemy to a point beside the player, instead of on it. The Main.myPlayer.getX/Y() in the first few lines would be replaced, but I don't know with what.

6. Member
Join Date
Feb 2012
Posts
20
Rep Power
0

Re: Math help

Since this post has drifted away to page 3, I may as well bump with a video showing how the enemies currently move.

Again, I just need a way to get a location off the side of the player. See the above graphic.