summaryrefslogtreecommitdiff
path: root/src/aufgaben/blatt2/Objekt.java
blob: 031b73a1700e6568c029777490aa9d8e844c0e94 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package aufgaben.blatt2;

import java.util.Date;

public class Objekt {
	
	private float x, y, dir, v;
	private long time;
	
	public Objekt(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public void setMovement(float degrees, float v) {
		this.dir = degrees;
		this.v = v;
		this.time = System.nanoTime();
	}
	
	public void update() {
		long delta = getDelta();
		float change = (float) (delta/1e9);
		if(dir == 0 || dir == 360) {
			x += change * v;
		} else if(dir >= 0 && dir < 90) {
			x += change * v;
			y -= Math.tan(Math.toRadians(dir)) * change * v;
		} else if(dir >= 90 && dir < 180) {
			x -= Math.tan(Math.toRadians(dir-90)) * change * v;
			y -= change * v;
		} else if(dir >= 180 && dir < 270) {
			x -= change * v;
			y += Math.tan(Math.toRadians(dir-180)) * change * v;
		} else if(dir >= 270 && dir < 360) {
			x += Math.tan(Math.toRadians(dir-270)) * change * v;
			y += change * v;
		} else {
			System.out.println("Invalid angle! (float, 0<=angle<=360)");
		}
		System.out.println("X = " + x + ", Y = " + y + ", fps = " + delta/1e9);
	}
	
	public int getX() {
		return Math.round(x);
	}
	
	public int getY() {
		return Math.round(y);
	}
	
	public long getDelta() {
		long old = time;
		time = System.nanoTime();
		return time-old;
	}
}