/
RectShape.java
45 lines (37 loc) · 1.26 KB
/
RectShape.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.gdc.gdcalaga;
import org.newdawn.slick.geom.Vector2f;
public class RectShape extends Shape {
public Vector2f pos, size;
/**
* @param position Takes a Vector2f that represents the position in x and y
* coordinates.
* @param size Takes a Vector2f that represents the size in x and y floats.
*/
RectShape(Vector2f position, Vector2f size){
type = ShapeType.Rectangle;
this.pos = new Vector2f(position);
this.size = new Vector2f(size);
}
public boolean Intersects(Shape other){
//Switching shape types allows for using different algorithms for different shapes
switch(other.type){
case Rectangle:
return Intersects((RectShape)other);
default:
return false;
}
}
private boolean Intersects(RectShape other){
if (other.pos.x + other.size.x / 2 < this.pos.x - this.size.x / 2
|| other.pos.x - other.size.x / 2 > this.pos.x + this.size.x / 2
|| other.pos.y + other.size.y / 2 < this.pos.y - this.size.y / 2
|| other.pos.y - other.size.y / 2 > this.pos.y + this.size.y / 2)
{
return false;
}
else
{
return true;
}
}
}