Results 1 to 6 of 6
Thread: Math help
 03042012, 12:50 PM #1Member
 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);
 03042012, 01:02 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,076
 Blog Entries
 7
 Rep Power
 24
Re: Math help
Where does the clear blue dotted line come from?
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03042012, 01:17 PM #3Member
 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
 03042012, 01:59 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,076
 Blog Entries
 7
 Rep Power
 24
 03052012, 01:48 AM #5Member
 Join Date
 Feb 2012
 Posts
 20
 Rep Power
 0
Re: Math help
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.
 03092012, 02:00 AM #6Member
 Join Date
 Feb 2012
 Posts
 20
 Rep Power
 0
Similar Threads

Doing Math
By nicholas205 in forum New To JavaReplies: 1Last Post: 01282012, 03:18 AM 
math and GUI
By urbanleg in forum AWT / SwingReplies: 3Last Post: 08062011, 04:05 PM 
Math.cos()
By ravi1 in forum New To JavaReplies: 5Last Post: 03272011, 02:52 PM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Math in java
By blug in forum New To JavaReplies: 2Last Post: 11222010, 03:21 PM
Bookmarks