StormyFrolic
StormyFrolic

Reputation: 3

Java Arc2D Collision detection (With Rotation)

I have tried to create NPC character that can "see" the player by using cones of vision. The NPC will rotate back and forth at all times.

My problem is that the arc has a generic and unchanging position, but when its drawn to the screen it looks correct. [Screenshots of the collisions in action][1] [GitHub link for java files][2]

I'm using Arc2D to draw the shape like this in my NPC class

// Update the shapes used in the npc
    rect.setRect(x, y, w, h);
    ellipse.setFrame(rect);
    visionArc.setArcByCenter(cx, cy, visionDistance, visionAngle, visionAngle * 2, Arc2D.PIE); 

/ CenterX, CenterY (of the npc), / the distance from the arc to the npc / a constant value around 45 degrees and a constant value around 90 degress (to make a pie shape)

I've tried multiplying the position and the angles by the sin and cosine of the NPC's current angle

Can you help me correct the actual positions of the arcs for my collision detection? I have tried creating new shapes so I can have one to do math on and one to draw to the screen but I scrapped that and am starting again from here. [1]: https://i.sstatic.net/rUvTM.png [2]: https://github.com/ShadowDraco/ArcCollisionDetection

Upvotes: 0

Views: 75

Answers (1)

StormyFrolic
StormyFrolic

Reputation: 3

After a few days of coding and learning and testing new ideas I came back to this program and implemented the collision detection using my original idea (ray casting) and have created the equivalent with rays! Screenshot of the new product Github link to the project that taught me the solution

  • Here's the new math
public void setRays() {
    for (int i = 0; i < rays.length; i++) {
        double rayStartAngleX = Math.sin(Math.toRadians((startAngle - angle) + i));
        double rayStartAngleY = Math.cos(Math.toRadians((startAngle - angle) + i));
        rays[i].setLine(cx, cy, cx + visionDistance * rayStartAngleX, cy + visionDistance * rayStartAngleY);
    }
}
  • Here is a link the the program I started after I asked this question and moved on to learn more, and an image to what the new product looks like (The original github page has been updated with a new branch :) I'm learning git hub right now too

  • I do not believe that using Arc2D in the way I intended is possible, however there is .setArcByTangent method, it may be possible to use that but I wasn't going to get into that. Rays are cooler.

Upvotes: 0

Related Questions