donderdag 7 april 2011

Point inside Polygon collision example

The Snippet/Example/Sample below shows you how to do a collision with Polygons. The Contains instruction checks if the given coordinates are in the given polygon. The sample below also shows you how to create a polygon and how to draw it.

 
import java.applet.Applet;
import java.awt.*;

public class Polygon1 extends Applet {

  public void paint (Graphics g) {
    int[] XArray = {20, 160, 120, 160, 20, 60};
    int[] YArray = {20, 20, 90, 160, 160, 90};
    Polygon abc = new Polygon(XArray, YArray, 6);
    g.fillPolygon(abc);
    if (abc.contains(30,30))
     g.drawString("Coordinates in Polygon", 20, 20 );;
  }

}

With this collision method I think you will be able to check if a game character is on a slope. I havent tried that yet though but I will.

If I understand the oracle documents then you can also call contains with 4 coordinates to do a rectangle vs polygon collision. At the moment I am looking how to check collision with two polygons.

Geen opmerkingen:

Een reactie posten