-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Obstacle & collision managers * fix rebase * vibration/mediaplayer manager * nighttimemanager * remove unused --------- Co-authored-by: github-mathieu-capo <[email protected]>
- Loading branch information
1 parent
75cb0fc
commit 345f352
Showing
7 changed files
with
356 additions
and
232 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
app/src/main/java/helloandroid/ut3/chauvequipeut/CollisionManager.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,48 @@ | ||
package helloandroid.ut3.chauvequipeut; | ||
|
||
import android.graphics.PointF; | ||
|
||
public class CollisionManager { | ||
|
||
|
||
public static boolean checkForCollision(int[][] bat, float[][] ob) { | ||
boolean toReturn = intersects(new PointF(bat[0][0],bat[0][1]), new PointF(bat[1][0],bat[1][1]), // verif collision haut bat avec gauche obstacle | ||
new PointF(ob[0][0],ob[0][1]), new PointF(ob[2][0],ob[2][1])) || | ||
|
||
intersects(new PointF(bat[1][0],bat[1][1]), new PointF(bat[3][0],bat[3][1]),// verif collision droite bat avec gauche obstacle | ||
new PointF(ob[0][0],ob[0][1]), new PointF(ob[2][0],ob[2][1])) || | ||
|
||
intersects(new PointF(bat[0][0],bat[0][1]), new PointF(bat[2][0],bat[2][1]),// verif collision gauche bat avec droite obstacle | ||
new PointF(ob[1][0],ob[1][1]), new PointF(ob[2][0],ob[2][1])) || | ||
|
||
intersects(new PointF(bat[2][0],bat[2][1]), new PointF(bat[3][0],bat[3][1]),// verif collision bas bat avec droite obstacle | ||
new PointF(ob[1][0],ob[1][1]), new PointF(ob[2][0],ob[2][1])); | ||
|
||
return toReturn; | ||
} | ||
static boolean intersects(PointF a1, PointF a2, PointF b1, PointF b2) { | ||
PointF intersection = new PointF(); | ||
|
||
PointF b = new PointF(a2.x - a1.x, a2.y - a1.y); | ||
PointF d = new PointF(b2.x - b1.x, b2.y - b1.y); | ||
float bDotDPerp = b.x * d.y - b.y * d.x; | ||
|
||
// if b dot d == 0, it means the lines are parallel so have infinite intersection points | ||
if (bDotDPerp == 0) | ||
return false; | ||
|
||
PointF c = new PointF(b1.x - a1.x, b1.y - a1.y); | ||
float t = (c.x * d.y - c.y * d.x) / bDotDPerp; | ||
if (t < 0 || t > 1) | ||
return false; | ||
|
||
float u = (c.x * b.y - c.y * b.x) / bDotDPerp; | ||
if (u < 0 || u > 1) | ||
return false; | ||
|
||
intersection.set(a1.x + b.x * t, a1.y + b.y * t); | ||
|
||
return true; | ||
} | ||
|
||
} |
Oops, something went wrong.