change ship x to pos.x and introduce speed

This commit is contained in:
Makussu 2023-12-19 13:27:41 +01:00
parent a26432dbdd
commit 4b2e32591f
2 changed files with 33 additions and 28 deletions

View File

@ -5,21 +5,21 @@
// Robi muss noch beschleunigen können
float checkDistance(Log log, Ship ship) {
float testX = ship.x;
float testY = ship.y;
float testX = ship.pos.x;
float testY = ship.pos.y;
// which edge is closest?
if (ship.x < log.x) testX = log.x;
else if (ship.x > log.x+log.logwidth) testX = log.x+log.logwidth;
if (ship.y < log.y) testY = log.y;
else if (ship.y > log.y+log.logheight) testY = log.y+log.logheight;
if (ship.pos.x < log.x) testX = log.x;
else if (ship.pos.x > log.x+log.logwidth) testX = log.x+log.logwidth;
if (ship.pos.y < log.y) testY = log.y;
else if (ship.pos.y > log.y+log.logheight) testY = log.y+log.logheight;
// get distant
float distX = ship.x-testX;
float distY = ship.y-testY;
float distX = ship.pos.x-testX;
float distY = ship.pos.y-testY;
float distance = sqrt( (distX*distX) + (distY*distY) );
line(testX, testY, ship.x, ship.y);
line(testX, testY, ship.pos.x, ship.pos.y);
// if the distance is less than the radius, collision!
return distance;

View File

@ -2,20 +2,25 @@
// TODO Abprallen nicht machen, wenn newx nicht erreicht sein kann, sondern wenn Robi tatsächlich die Wand berührt
class Ship {
float x, y, a;
PVector pos = new PVector();
float a;
float newX, newY;
boolean colliding = false;
boolean colliding2 = false;
boolean hasSaw = false;
float rotationSpeed = 4; // The speed of rotation
float movementSpeed = 2; // The speed of movement
float speed; // The speed of movement
float maxSpeed = 4;
float minSpeed = 2;
int nextLog;
int health = 3;
Ship() {
x = width / 2;
y = height / 2;
pos.x = width/2;
pos.y = height/2;
a = -90;
newX = width/2;
newY = height/2;
@ -43,11 +48,11 @@ class Ship {
if (hasSaw) {
if (checkDistance(logs[nextLog], ship_zero) < 40) {
fill(204, 102, 0);
circle(x, y -20, 20);
circle(pos.x, pos.y -20, 20);
fill(0);
textSize(32);
textAlign(CENTER, CENTER);
text("s", x, y - 27);
text("s", pos.x, pos.y - 27);
}
}
}
@ -79,28 +84,28 @@ class Ship {
}
if (NORTH) {
newX = x + cos(radians(a)) * movementSpeed;
newY = y + sin(radians(a)) * movementSpeed;
newX = pos.x + cos(radians(a)) * speed;
newY = pos.y + sin(radians(a)) * speed;
// Check if the new position is within the bounds of the screen
if(colliding == false && colliding2 == false) {
x = newX;
y = newY;
pos.x = newX;
pos.y = newY;
} else {
float richtungX = x-newX;
float richtungY = y-newY;
x = (7*richtungX) + x;
y = (7*richtungY) + y;
float richtungX = pos.x-newX;
float richtungY = pos.y-newY;
pos.x = (7*richtungX) + pos.x;
pos.y = (7*richtungY) + pos.y;
health = health - 1;
}
} else if (SOUTH) {
newX = x - cos(radians(a)) * movementSpeed;
newY = y - sin(radians(a)) * movementSpeed;
newX = pos.x - cos(radians(a)) * speed;
newY = pos.y - sin(radians(a)) * speed;
// Check if the new position is within the bounds of the screen
if(colliding == false && colliding2 == false) {
x = newX;
y = newY;
pos.x = newX;
pos.y = newY;
} //else {
// x = newX - x;
// y = newY - y;
@ -127,7 +132,7 @@ class Ship {
void render() {
pushMatrix();
translate(x, y);
translate(pos.x, pos.y);
rotate(radians(a + 90));
stroke(255);
noFill();