Kollisionserkennung auch mit anderen Formen möglich?

jeipack

Erfahrenes Mitglied
Hi
Ich habe eine kleine Kollisionserkennung:
Java:
private Rectangle me = new Rectangle();
private Rectangle him = new Rectangle();

public boolean collidesWith(Entity other) {
	me.setBounds((int) x,(int) y,sprite.getWidth(),sprite.getHeight());
	him.setBounds((int) other.x,(int) other.y,other.sprite.getWidth(),other.sprite.getHeight());

	return me.intersects(him);
}
Dank der Funktion intersects ist das recht einfach.
Nun würde ich das auch gerne mit anderen geometrischen Formen machen. Speziell mit einem Dreieck und mit einem Kreis. In java.awt finde ich aber keine anderen Formen..

--> Gibt es eine Möglichkeit Rechtecke, Kreise und Dreiecke einfach auf Kollision zu erkennen?

Vielen Dank schonmal
Gruss
jeipack
 
Grüß Dich jeipack,
Leider ist die eingebaute Polygonklasse vom Java-Standard nicht so klasse und kennt nur Bounds, wie Du schon selbst vermutlich gesehen hast.
Ich habe aber eine ganz nette Seite gefunden von einem aus Übersee (war zumindest auch ein Prof in einer Uni) Finnegan Southey, der da in seinem 2 Dimensionalen Roboterspiel eine Polygonklasse entwickelt hat, die Kollision mit Hilfe der tatsächlichen Seiten erkennt.

Offenbar ist diese durchausnützliche bibliothek nicht mehr auf seine Home Page, zumindest habe ich sie nicht finden können :-/

ich habe aber trotzdem diese Bibliothek mit ein bissle Zusatz von mir in besagter Polygon-Klasse.


viel Spaß damit :)

Takidoso
 
Nachtrag: leider kriege ich die Bibliothek nicht hochgeladen :-(
hier die beiden Klassen die Du benötigst:
Point2D
Java:
package ca.uoguelph.squire;

/**
 * A class that describes a point in 2-dimensional space using double precision
 * floating point.
 *
 * @author Finnegan Southey
 * @version $Name:  $ $Revision: 1.11 $, $Date: 2000/11/02 20:45:23 $
 * @legal Copyright (c) Finnegan Southey, 1996-1999
 *	This program is free software; you can redistribute it and/or modify it 
 *	under the terms of the GNU Library General Public License as published 
 *	by the Free Software Foundation; either version 2 of the License, or 
 *	(at your option) any later version.  This program is distributed in the 
 *	hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *	See the GNU Library General Public License for more details.  You should 
 *	have received a copy of the GNU Library General Public License along 
 *	with this program; if not, write to the Free Software Foundation, Inc., 
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 */
public final class Point2D
{

	/** The x coordinate of this point. **/
	public double	x;
	/** The y coordinate of this point. **/
	public double	y;

	/**
	 * Constructs a new point that has the coordinates (0, 0).
	 */
	public Point2D()
	{
	}
	/**
	 * Constructs a new point that has the specified x and y coordinates.
	 *
	 * @param newX  the x coordinate of the new point.
	 * @param newY  the y coordinate of the new point.
	 */
	public Point2D(double newX, double newY)
	{
		x = newX;
		y = newY;
	}
	/**
	 * Constructs a new point that has same coordinates as the specified point.
	 *
	 * @param point  the point from which to copy the coordinates.
	 */
	public Point2D(Point2D point)
	{
		this(point.x, point.y);
	}
	/**
	 * Sets this coordinates to be the same as that of the specified point.
	 *
	 * @param point  the point from which to copy the coordinates.
	 */
	public void copyFrom(Point2D point)
	{
		x = point.x;
		y = point.y;
	}
	/**
	 * Returns the x coordinate of this point.
	 *
	 * @return the x coordinate of this point.
	 */
	public double getX()
	{
		return x;
	}
	/**
	 * Returns the y coordinate of this point.
	 *
	 * @return the y coordinate of this point.
	 */
	public double getY()
	{
		return y;
	}
	/**
	 * Sets the x and y coordinates of this point to the specified values.
	 *
	 * @param newX  the x coordinate of this point.
	 * @param newY  the y coordinate of this point.
	 */
	public void setCoordinates(double newX, double newY)
	{
		x = newX;
		y = newY;
	}
	/**
	 * Translates this point by the specified values.
	 * Translation acts like x = x + dx and y = y + dy.
	 *
	 * @param dx  the distance along the axis to translate the x coordinate of this point.
	 * @param dy  the distance along the axis to translate the y coordinate of this point.
	 */
	public void translate(double dx, double dy)
	{
		x += dx;
		y += dy;
	}
	/**
	 * Translates this point by summing it with the specified point.
	 * Translation acts like x = x + d.x and y = y + d.y.
	 *
	 * @param d  the point containing the offsets.
	 */
	public void translate(Point2D d)
	{
		x += d.x;
		y += d.y;
	}
	
	/**
	 * A T T E TN T I O N : this routines might be a break in Philosophy
	 *                      and migth get erased again 
	 * translate point with in a "sized world". 
	 * The edges on the x-axes are touching each other as well
	 * as the edges of the y-axes. So it is someting like a tube Ring
	 *  This routine was put in 4th of May 2008
	 * @param d the point containing the offsets.
	 * @param worldSizePoint the point giving the may x and y value of the imagined world
	 */
	public void translateWithinTubeRing(Point2D d, Point2D worldSizePoint)
	{
		x =  Math.abs(worldSizePoint.x % (x + d.x));
		y =  Math.abs(worldSizePoint.y % (y + d.y));
	}
	
	/**
	 * A T T E TN T I O N : this routines might be a break in Philosophy
	 *                      and migth get erased again
	 * translate point with in a "sized world". 
	 * The edges on the x-axes are touching each other as well
	 * as the edges of the y-axes. So it is someting like a tube ring
	 *  This routine was put in 4th of May 2008
	 * @param dx  the distance along the axis to translate the x coordinate of this point.
	 * @param dy  the distance along the axis to translate the y coordinate of this point.
	 * @param worldSizePoint the point giving the may x and y value of the imagined world
	 */
	public void translateWithinTubeRing(double dx, double dy, Point2D worldSizePoint)
	{
		x =  Math.abs(worldSizePoint.x % (x + dx));
		y =  Math.abs(worldSizePoint.y % (y + dy));
	}

	/**
	 * A T T E TN T I O N : this routines might be a break in Philosophy
	 *                      and migth get erased again
	 * translate point with in a "sized world". 
	 * The edges on the x-axes are touching each other as well
	 * as the edges of the y-axes. So it is someting like a tube Ring
	 *  This routine was put in 4th of May 2008
	 * @param dx  the distance along the axis to translate the x coordinate of this point.
	 * @param dy  the distance along the axis to translate the y coordinate of this point.
	 * @param wolrdSizeX witdth of the imagined world
	 * @param wolrdSizeX witdth of the imagined world
	 */
	public void translateWithinTubeRing(double dx, double dy, double wolrdSizeX, double worldSizeY)
	{
		x =  Math.abs(wolrdSizeX % (x + dx));
		y =  Math.abs(worldSizeY % (y + dy));
	}
	
	
	
	/**
	 * Rotates this point around the specified center point by the specified angle.
	 *
	 * @param centerX  the x coordinate of the point around which to rotate this point.
	 * @param centerY  the y coordinate of the point around which to rotate this point.
	 * @param angle  the angle, in radians, of the rotation.
	 */
	public void rotate(double centerX, double centerY, double angle)
	{
		double tx = x - centerX;
		double ty = y - centerY;
		double cosA, sinA;
		cosA = Math.cos(angle);
		sinA = Math.sin(angle);
		x = tx * cosA - ty * sinA + centerX;
		y = tx * sinA + ty * cosA + centerY;
	}
	/**
	 * Rotates this point around the specified center point by the specified angle.
	 *
	 * @param centerPoint  the Point2D around which to rotate this point.
	 * @param angle  the angle, in radians, of the rotation.
	 */
	public void rotate(Point2D centerPoint, double angle)
	{
		rotate(centerPoint.x, centerPoint.y, angle);
	}
	/**
	 * Rotates this point around (0, 0) by the specified angle.
	 *
	 * @param angle  the angle, in radians, of the rotation.
	 */
	public void rotate(double angle)
	{
		double tx = x;
		double ty = y;
		double cosA, sinA;
		cosA = Math.cos(angle);
		sinA = Math.sin(angle);
		x = tx * cosA - ty * sinA;
		y = tx * sinA + ty * cosA;
	}
	/**
	 * Returns the distance from this point to the specified point.
	 *
	 * @param targetX  the x coordinate of the point whose distance from this point is 
	 * to be computed.
	 * @param targetY  the y coordinate of the point whose distance from this point is 
	 * to be computed.
	 * @return the computed distance.
	 */
	public double getDistanceTo(double targetX, double targetY)
	{
		double dx, dy;
		dx = targetX - x;
		dy = targetY - y;
		return Math.sqrt(dx * dx + dy * dy);
	}
	/**
	 * Returns the distance from this point to the specified point.
	 *
	 * @param point  the point whose distance from this point is to be computed.
	 * @return the computed distance.
	 */
	public double getDistanceTo(Point2D point)
	{
		double dx, dy;
		dx = point.x - x;
		dy = point.y - y;
		return Math.sqrt(dx * dx + dy * dy);
	}
	/**
	 * Returns the squared distance from this point to the specified point.
	 * This is provided because the squared distance may be calculated faster than
	 * the true distance and may be sufficient for many cases.
	 *
	 * @param point  the point whose squared distance from this point is to be computed.
	 * @return the computed squared distance.
	 */
	public double getSquaredDistanceTo(Point2D point)
	{
		double dx, dy;
		dx = point.x - x;
		dy = point.y - y;
		return dx * dx + dy * dy;
	}
	/**
	 * Returns the squared distance from this point to the specified point.
	 * This is provided because the squared distance may be calculated faster than
	 * the true distance and may be sufficient for many cases.
	 *
	 * @param targetX  the x coordinate of the point whose distance from this point is 
	 * to be computed.
	 * @param targetY  the y coordinate of the point whose distance from this point is 
	 * to be computed.
	 * @return the computed squared distance.
	 */
	public double getSquaredDistanceTo(double targetX, double targetY)
	{
		double dx, dy;
		dx = targetX - x;
		dy = targetY - y;
		return dx * dx + dy * dy;
	}
	public String toString()
	{
		return "(" + x + "," + y + ")";
	}
	/**
	 * Returns true if the this point has the same coordinates as the specified point.
	 *
	 * @return true if the this point has the same coordinates as the specified point.
	 */
	public boolean equals(Point2D otherPoint)
	{
		return ( x == otherPoint.x ) && ( y == otherPoint.y );
	}
}
 
soo nu habe ich mal als txt die entscheidenen Java-Klassen hochgeladen, da die polygonklasse offenbar auch zu groß war um hier im text dargestellt zu werden.
 

Anhänge

  • Polygon2D.TXT
    35,5 KB · Aufrufe: 32
  • Point2D.TXT
    8,8 KB · Aufrufe: 19
Die Polygonklasse sieht nicht schlecht aus ;)
Erstmal vielen Dank!

..Antworte dir richtig wenn ich dazugekommen bin alles mal durchzutesten..
 
Hi Jeipack,
ich habe endlich mal wieder die Seite wiedergefunden, mit dem Robotterspiel, woher die Polygonklasse ist.
Squire-Roboterspiel
Ist ein Link ausvder HP von Finnegan Southey. Er war noch vo einigen Jahren Dozent und hat offenbar nun dort keine Seite mehr, die ich ursprünglich 2006 recherschierte.

viel Spaß damit.
Takidoso


PS: Achja, vielen Dank nochmal für den Link zu Deinem Prototypen, fand ich super. Habe Dir diesbezügich auch eine PM zugesandt.
 
Hi Jeipack,
ich habe zwar mit dieser Klasse noch nicht selbst gearbeitet, aber ich kann mir vorstellen,, dass die Klasse Platform vielleicht auch Body (oder so ähnlich) aus dem Roboterspiel Squire Dir noch weitere Abstraktion gibt.

Ich selbst habe halt nur mit Polygon gebastelt, da es bei meinem Spielchen nur um "Ländergrenzen" geht, die vom Benutzer ankclickbar werden sollten, also keine Beweglichen Figuren, wie bei Dir.

Grüße

Takidoso

PS: Hey wie wäre es mal mit einer genialen Spielidee: wie wäre es wenn man ein Raumschiff mit 2 Spielern steuert, einer der das Raumschiff fährt, der andere der eine schwenkbare Kanone bedient :-D
Wenn Du sowas hinbekommst, wäre ich Dein größter Fan ;-)
 
Hey
Ja die Klasse ist super. Habs jetzt aber anderst gemacht. Mit einer sogenanten Pixelgenauen Kollisionserkennung..

PS: Hey wie wäre es mal mit einer genialen Spielidee: wie wäre es wenn man ein Raumschiff mit 2 Spielern steuert, einer der das Raumschiff fährt, der andere der eine schwenkbare Kanone bedient :-D
Wenn Du sowas hinbekommst, wäre ich Dein größter Fan ;-)

Hehe gar nicht mal schlecht. Mir fehlt es halt eh an einer gute Spielidee.


Gruss
 
Hey
Ja die Klasse ist super. Habs jetzt aber anderst gemacht. Mit einer sogenanten Pixelgenauen Kollisionserkennung..
Die Polygon2D Klasse aus Squire, ist AFAIK pixelgenau.
auf welche Weise hast Du es denn dann gelöst (mal ganz neugierig frag)?

Hehe gar nicht mal schlecht. Mir fehlt es halt eh an einer gute Spielidee.

Mein problem sind weniger die Spielideen, vielmehr das Aufrechterhalten der motivation (grrrrr)
 

Neue Beiträge

Zurück