// Constants for the field dimensions and goals
const FIELD_WIDTH = 260;
const FIELD_HEIGHT = 120;
const GOAL_WIDTH = 40;
const GOAL_HEIGHT = 20;
// Constants for the car dimensions and physics
const CAR_WIDTH = 20;
const CAR_HEIGHT = 10;
const CAR_MASS = 1;
const CAR_MAX_SPEED = 30;
const CAR_ACCELERATION = 10;
const CAR_TURN_RATE = 10;
// Constants for the ball dimensions and physics
const BALL_RADIUS = 5;
const BALL_MASS = 1;
const BALL_MAX_SPEED = 50;
// The AI class that controls the car
class AI {
constructor(car) {
this.car = car;
}
// This function is called by the game loop at a fixed rate
update(dt) {
// Get a reference to the ball and the current position of the car
const ball = game.ball;
const carPos = this.car.pos;
// Calculate the distance between the car and the ball
const distToBall = Math.sqrt(
Math.pow(carPos.x - ball.pos.x, 2) +
Math.pow(carPos.y - ball.pos.y, 2)
);
// If the car is close to the ball, try to kick it towards the goal
if (distToBall < 50) {
this.kickTowardsGoal();
} else {
// If the ball is on the other side of the field, drive towards it
this.driveTowardsBall();
}
// If the ball is in range of the goal, try to defend the goal
if (this.ballInRangeOfGoal()) {
this.defendGoal();
}
// Update the car's position and velocity based on its acceleration and turn rate
this.car.update(dt);
}
// Try to kick the ball towards the goal
kickTowardsGoal() {
// Calculate the direction to the goal
const goalPos = game.goalPos;
const ballPos = game.ball.pos;
const dirToGoal = {
x: goalPos.x - ballPos.x,
y: goalPos.y - ballPos.y
};
// Normalize the direction vector
const distToGoal = Math.sqrt(dirToGoal.x * dirToGoal.x + dirToGoal.y * dirToGoal.y);
dirToGoal.x /= distToGoal;
dirToGoal.y /= distToGoal;
// Kick the ball in the direction of the goal
game.ball.applyForce(dirToGoal.x * 1000, dirToGoal.y * 1000);
}
// Drive the car towards the ball
driveTowardsBall() {
// Calculate the direction to the ball
const ballPos = game.ball.pos;
const carPos = this.car.pos;
const dirTo
ball = {
x: ballPos.x - carPos.x,
y: ballPos.y - carPos.y
};
// Normalize the direction vector
const distToBall = Math.sqrt(dirToBall.x * dirToBall.x + dirToBall.y * dirToBall.y);
dirToBall.x /= distToBall;
dirToBall.y /= distToBall;
// Set the car's acceleration and turn rate based on the direction to the ball
this.car.acceleration = dirToBall.x * CAR_ACCELERATION;
this.car.turnRate = dirToBall.y * CAR_TURN_RATE;
}
// Check if the ball is in range of the goal
ballInRangeOfGoal() {
// Calculate the distance between the ball and the center of the goal
const ballPos = game.ball.pos;
const goalPos = game.goalPos;
const distToGoal = Math.sqrt(
Math.pow(ballPos.x - goalPos.x, 2) +
Math.pow(ballPos.y - goalPos.y, 2)
);
// Check if the ball is within range of the goal
return distToGoal < GOAL_WIDTH / 2 + BALL_RADIUS;
}
// Try to defend the goal
defendGoal() {
// Calculate the direction to the ball
const ballPos = game.ball.pos;
const carPos = this.car.pos;
const dirToBall = {
x: ballPos.x - carPos.x,
y: ballPos.y - carPos.y
};
// Normalize the direction vector
const distToBall = Math.sqrt(dirToBall.x * dirToBall.x + dirToBall.y * dirToBall.y);
dirToBall.x /= distToBall;
dirToBall.y /= distToBall;
// Set the car's acceleration and turn rate based on the direction to the ball
this.car.acceleration = -dirToBall.x * CAR_ACCELERATION;
this.car.turnRate = -dirToBall.y * CAR_TURN_RATE;
}
}
// The game object that simulates the physics and manages the game state
const game = {
// The ball object that represents the ball in the game
ball: {
pos: { x: 0, y: 0 },
vel: { x: 0, y: 0 },
radius: BALL_RADIUS,
mass: BALL_MASS,
maxSpeed: BALL_MAX_SPEED,
// Apply a force to the ball
applyForce(fx, fy) {
this.vel.x += fx / this.mass;
this.vel.y += fy / this.mass;
// Clamp the ball's speed to the maximum speed
const speed = Math.sqrt(this.vel.x * this.vel.x + this.vel.y * this.vel.y);
if (speed >
this.maxSpeed) {
this.vel.x = this.vel.x / speed * this.maxSpeed;
this.vel.y = this.vel.y / speed * this.maxSpeed;
}
},
// Update the ball's position based on its velocity
update(dt) {
this.pos.x += this.vel.x * dt;
this.pos.y += this.vel.y * dt;
}
},
// The car object that represents the player's car in the game
car: {
pos: { x: 0, y: 0 },
vel: { x: 0, y: 0 },
width: CAR_WIDTH,
height: CAR_HEIGHT,
mass: CAR_MASS,
maxSpeed: CAR_MAX_SPEED,
acceleration: 0,
turnRate: 0,
// Update the car's position and velocity based on its acceleration and turn rate
update(dt) {
this.vel.x += this.acceleration * dt;
this.vel.y += this.turnRate * dt;
// Clamp the car's speed to the maximum speed
const speed = Math.sqrt(this.vel.x * this.vel.x + this.vel.y * this.vel.y);
if (speed > this.maxSpeed) {
this.vel.x = this.vel.x / speed * this.maxSpeed;
this.vel.y = this.vel.y / speed * this.maxSpeed;
}
this.pos.x += this.vel.x * dt;
this.pos.y += this.vel.y * dt;
}
},
// The position of the goal
goalPos: { x: 0, y: 0 },
// The AI that controls the car
ai: new AI(game.car),
// The game loop that updates the game state and renders the game
loop(dt) {
// Update the ball and car positions based on their velocities
this.ball.update(dt);
this.ai.update(dt);
// Render the game
this.render();
},
// Render the game
render() {
// Clear the screen
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw the ball
context.beginPath();
context.arc(this.ball.pos.x, this.ball.pos.y, this.ball.radius, 0, 2 * Math.PI);
context.fillStyle = "white";
context.fill();
// Draw the car
context.fillRect(this.car.pos.x - this.car.width / 2, this.car.pos.y - this.car.height / 2, this.car.width, this.car.height);
// Draw the goal
context.fillRect(this.goalPos.x - GOAL_WIDTH / 2, this.goalPos.y - GOAL_HEIGHT / 2, GOAL_WIDTH, GOAL_HEIGHT);
}
};
// Set