Results 1 to 6 of 6

Thread: Math help

  1. #1
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Post 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.

    Math help-math-help.png



    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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Math help

    Where does the clear blue dotted line come from?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Math help

    Quote Originally Posted by stuffses View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Math help

    Quote Originally Posted by JosAH View Post
    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. #6
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default 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.

Similar Threads

  1. Doing Math
    By nicholas205 in forum New To Java
    Replies: 1
    Last Post: 01-28-2012, 02:18 AM
  2. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 04:05 PM
  3. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:52 PM
  4. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  5. Math in java
    By blug in forum New To Java
    Replies: 2
    Last Post: 11-22-2010, 02:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •