-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
early commit of collision detection for fastJ physics
- Loading branch information
1 parent
e31abe5
commit 78e5838
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package tech.fastj.physics; | ||
|
||
public abstract class Collider { | ||
public abstract boolean checkCollision(Collider other); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package tech.fastj.physics; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CollisionManager { | ||
private List<Collider> colliders = new ArrayList<>(); | ||
|
||
public void addCollider(Collider collider) { | ||
colliders.add(collider); | ||
} | ||
|
||
public boolean checkCollisions() { | ||
// Ελέγχουμε για σύγκρουση μεταξύ όλων των colliders | ||
for (int i = 0; i < colliders.size(); i++) { | ||
for (int j = i + 1; j < colliders.size(); j++) { | ||
if (colliders.get(i).checkCollision(colliders.get(j))) { | ||
System.out.println("Collision detected between collider " + i + " and collider " + j); | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tech.fastj.physics; | ||
|
||
import java.awt.geom.Rectangle2D; | ||
|
||
|
||
public class RectangleCollider extends Collider { | ||
private final Rectangle2D shape; | ||
|
||
public RectangleCollider(Rectangle2D shape) { | ||
this.shape = shape; | ||
} | ||
|
||
@Override | ||
public boolean checkCollision(Collider other) { | ||
if (other instanceof RectangleCollider) { | ||
RectangleCollider otherRectangle = (RectangleCollider) other; | ||
|
||
// Έλεγχος αν τα δύο ορθογώνια επικαλύπτονται | ||
return shape.intersects(otherRectangle.shape); | ||
} | ||
return false; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/unittest/testcases/physics/CollisionManagerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package unittest.testcases.physics; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import java.awt.geom.Rectangle2D; | ||
import tech.fastj.physics.RectangleCollider; | ||
import tech.fastj.physics.CollisionManager; | ||
|
||
public class CollisionManagerTests { | ||
|
||
@Test | ||
void testCollisionDetection() { | ||
// Δημιουργία 2 colliders με επικαλυπτόμενα rectangles | ||
RectangleCollider collider1 = new RectangleCollider(new Rectangle2D.Double(0, 0, 50, 50)); | ||
RectangleCollider collider2 = new RectangleCollider(new Rectangle2D.Double(25, 25, 50, 50)); | ||
|
||
// Δημιουργία CollisionManager | ||
CollisionManager collisionManager = new CollisionManager(); | ||
collisionManager.addCollider(collider1); | ||
collisionManager.addCollider(collider2); | ||
|
||
// Έλεγχος αν υπάρχει σύγκρουση | ||
collisionManager.checkCollisions(); // Αναμένεται να εμφανίσει μήνυμα για σύγκρουση | ||
} | ||
|
||
@Test | ||
void testNoCollision() { | ||
// Δημιουργία 2 colliders χωρίς επικαλυπτόμενα rectangles | ||
RectangleCollider collider1 = new RectangleCollider(new Rectangle2D.Double(0, 0, 50, 50)); | ||
RectangleCollider collider2 = new RectangleCollider(new Rectangle2D.Double(100, 100, 50, 50)); | ||
|
||
// Δημιουργία CollisionManager | ||
CollisionManager collisionManager = new CollisionManager(); | ||
collisionManager.addCollider(collider1); | ||
collisionManager.addCollider(collider2); | ||
|
||
// Έλεγχος αν δεν υπάρχει σύγκρουση | ||
collisionManager.checkCollisions(); // Δεν πρέπει να εμφανίσει μήνυμα σύγκρουσης | ||
} | ||
} |