Math help

Printable View

• 03-04-2012, 12:50 PM
stuffses
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.

Attachment 3161

If this helps, here is the current AI code:

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);```
• 03-04-2012, 01:02 PM
JosAH
Re: Math help
Where does the clear blue dotted line come from?

kind regards,

Jos
• 03-04-2012, 01:17 PM
stuffses
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
• 03-04-2012, 01:59 PM
JosAH
Re: Math help
Quote:

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
• 03-05-2012, 01:48 AM
stuffses
Re: Math help
Quote:

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.
• 03-09-2012, 02:00 AM
stuffses
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.

http://www.youtube.com/watch?v=gZ-IR-m3JgA

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