import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.util.Vector;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
public class Car {
private Vector2f position;
private int width, height;
private float acceleration, deceleration;
private float maxVelocity;
private float drag;
private float carHeading;
private float carVelocity;
private float steerVelocity;
private float steerAngle;
private float maxSteeringAngle;
private float wheelBase;
private boolean frontDirection = true;
Line lineAB,lineBC,lineCD,lineDA;
Point A,B,C,D;
private Vector<Line> lines = new Vector<Line>();
public Car(int x, int y, int width, int height, float wheelBase, float carHeading, float acceleration, float deceleration, float maxVelocity, float drag, float steerVelocity, float maxSteeringAngle) {
this.position = new Vector2f(x+width/2, y+height/2);
this.width = width;
this.height = height;
this.wheelBase = wheelBase;
this.carHeading = carHeading;
this.acceleration = acceleration;
this.deceleration = deceleration;
this.maxVelocity = maxVelocity;
this.drag = drag;
this.steerVelocity = steerVelocity;
this.maxSteeringAngle = maxSteeringAngle;
this.steerAngle = 0;
setBoundingBox();
}
public void setBoundingBox(){
A = new Point((int)position.x-width/2,(int)position.y-height/2);
B = new Point((int)position.x+width/2,(int)position.y-height/2);
C = new Point((int)position.x+width/2,(int)position.y+height/2);
D = new Point((int)position.x-width/2,(int)position.y+height/2);
lineAB = new Line();
lineAB.setLine(A, B);
lines.addElement(lineAB);
lineBC = new Line();
lineBC.setLine(B, C);
lines.addElement(lineBC);
lineCD = new Line();
lineCD.setLine(C, D);
lines.addElement(lineCD);
lineDA = new Line();
lineDA.setLine(D, A);
lines.addElement(lineDA);
}
public boolean intersects(Vector<Line> objLines){
for (int i = 0; i < objLines.size(); i++) {
for (int j = 0; j < lines.size(); j++) {
if(objLines.get(i).intersectsLine(lines.get(j))){
return true;
}
}
}
return false;
}
public Vector<Line> getLines(){
return lines;
}
public Vector2f getPosition() {
return position;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public float getAcceleration() {
return acceleration;
}
public float getDeceleration() {
return deceleration;
}
public float getWheelBase() {
return wheelBase;
}
public float getCarHeading() {
return carHeading;
}
public void setCarHeading(float carHeading) {
this.carHeading = carHeading;
}
public float getCarVelocity() {
return carVelocity;
}
public void setCarVelocity(float carVelocity) {
this.carVelocity = carVelocity;
}
public float getSteerVelocity() {
return steerVelocity;
}
public float getMaxSteeringAngle() {
return maxSteeringAngle;
}
public float getSteerAngle() {
return steerAngle;
}
public void setSteerAngle(float steerAngle) {
this.steerAngle = steerAngle;
}
public boolean isFrontDirection() {
return frontDirection;
}
public void setFrontDirection(boolean frontDirection) {
this.frontDirection = frontDirection;
}
public void setPosition(Vector2f position) {
this.position = position;
}
public float getDrag() {
return drag;
}
public float getMaxVelocity() {
return maxVelocity;
}
public String toString() {
return "Acc: " + acceleration + " Vel: " + carVelocity + " MaxVel:" + maxVelocity;
}
public void show(){
AffineTransform at = new AffineTransform();
at.translate(position.x, position.y);
at.rotate(Math.toRadians(carHeading));
at.translate(-position.x, -position.y);
at.transform(A, A);
at.transform(B, B);
at.transform(C, C);
at.transform(D, D);
// A.move((int)position.x-width/2, (int)position.y-height/2);
// B.move((int)position.x+width/2, (int)position.y-height/2);
// C.move((int)position.x+width/2, (int)position.y+height/2);
// D.move((int)position.x-width/2, (int)position.y+height/2);
lineAB.setLine(A, B);
lineBC.setLine(B, C);
lineCD.setLine(C, D);
lineDA.setLine(D, A);
// Wheel matrix
GL11.glColor3f(0f, 255f, 255f);
GL11.glPushMatrix();
GL11.glTranslatef(getPosition().x, getPosition().y, 0);
GL11.glRotatef((float)Math.toDegrees(getCarHeading()), 0f, 0f, 1f);
GL11.glTranslatef(-getPosition().x, -getPosition().y, 0);
GL11.glTranslatef(getPosition().x + (getWheelBase()/2), getPosition().y - (getHeight()/2) + 7, 0);
GL11.glRotatef((float)Math.toDegrees(getSteerAngle()), 0f, 0f, 1f);
GL11.glTranslatef(-(getPosition().x + (getWheelBase()/2)), -(getPosition().y - (getHeight()/2) + 7), 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) - 10, getPosition().y - (getHeight()/2));
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) + 10 , getPosition().y - (getHeight()/2));
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) + 10 , getPosition().y - (getHeight()/2) + 10);
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) - 10, getPosition().y - (getHeight()/2) + 10);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glColor3f(0f, 255f, 255f);
GL11.glPushMatrix();
GL11.glTranslatef(getPosition().x, getPosition().y, 0);
GL11.glRotatef((float)Math.toDegrees(getCarHeading()), 0f, 0f, 1f);
GL11.glTranslatef(-getPosition().x, -getPosition().y, 0);
GL11.glTranslatef(getPosition().x + (getWheelBase()/2), getPosition().y + (getHeight()/2) - 7, 0);
GL11.glRotatef((float)Math.toDegrees(getSteerAngle()), 0f, 0f, 1f);
GL11.glTranslatef(-(getPosition().x + (getWheelBase()/2)), -(getPosition().y + (getHeight()/2) - 7), 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) - 10, getPosition().y + (getHeight()/2));
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) + 10 , getPosition().y + (getHeight()/2));
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) + 10 , getPosition().y + (getHeight()/2) - 10);
GL11.glVertex2f(getPosition().x + (getWheelBase()/2) - 10, getPosition().y + (getHeight()/2) - 10);
GL11.glEnd();
GL11.glPopMatrix();
// Wheel matrix end
// Car matrix
GL11.glColor3f(255f, 255f, 255f);
GL11.glPushMatrix();
GL11.glTranslatef(getPosition().x, getPosition().y, 0);
GL11.glRotatef((float)Math.toDegrees(getCarHeading()), 0f, 0f, 1f);
GL11.glTranslatef(-getPosition().x, -getPosition().y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(getPosition().x - getWidth()/2, getPosition().y - getHeight()/2);
GL11.glVertex2f(getPosition().x + getWidth()/2, getPosition().y - getHeight()/2);
GL11.glVertex2f(getPosition().x + getWidth()/2, getPosition().y + getHeight()/2);
GL11.glVertex2f(getPosition().x - getWidth()/2, getPosition().y + getHeight()/2);
GL11.glEnd();
GL11.glPopMatrix();
// BOUND
GL11.glColor3f(0f, 255f, 255f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(A.x,A.y);
GL11.glVertex2f(B.x,B.y);
GL11.glVertex2f(C.x,C.y);
GL11.glVertex2f(D.x,D.y);
GL11.glEnd();
GL11.glPopMatrix();
// Car matrix end
// Refresh Bounds
// Refresh Bounds end
}
}