Skip to content

Commit

Permalink
NOW THE CAR WHEEL MOVE IN BOTH DIRECTION LIKE REAL CAR
Browse files Browse the repository at this point in the history
  • Loading branch information
THEKINGSTAR committed Dec 9, 2023
1 parent 52d9e27 commit 5d392c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
44 changes: 31 additions & 13 deletions main_project/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Car{
//define the car speed and direction based on physics
this.speed=0;
this.acceleration=0.2;
this.maxSpeed=4;
this.maxSpeed=3;
this.friction=0.05;
this.angel=0;

Expand Down Expand Up @@ -43,27 +43,45 @@ class Car{
this.speed=0;
}

//Set the controls for left and right
//By modifying the car angel not just go to left or right side of the screen
if(this.controls.right){
this.angel-=0.03;
}
if(this.controls.left){
this.angel+=0.-3;

//SET THE DIRECTION OF THE WHEEL BASE ON THE DIRECTION OF THE CAR
if(this.speed!=0){
const flip = this.speed>0?1:-1;
/*
Set the controls for left and right
By modifying the car angel not just go to left or right side of the screen
*/
if(this.controls.left){
this.angel+=0.03 * flip;
}
if(this.controls.right){
this.angel-=0.03 * flip;
}
}


this.y-=this.speed;
}
/*
MAKE THE CAR MOVE IN THE DIRECTION OF THE ROTATION BASE ON
SIN OR COS OF THE ANGLE TO THE (ZERO, ZERO) AXIS
*/
this.x-=Math.sin(this.angel) * this.speed;
this.y-=Math.cos(this.angel) * this.speed;

}
//Draw the canvas and the car on it
draw(ctx){
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(-this.angel);

ctx.beginPath();
ctx.rect(
this.x - this.width / 2,
this.y - this.height / 2,
-this.width / 2,
-this.height / 2,
this.width,
this.height
);
ctx.fill();

ctx.restore();
}
}
8 changes: 4 additions & 4 deletions main_project/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Controls{
this.reverse=true;
break;
}
//Check for event caching the keys
console.table(this);
//Check for event it catching the keys
//console.table(this);
}
document.onkeyup=(event)=>{
switch(event.key)
Expand All @@ -44,8 +44,8 @@ class Controls{
this.reverse=false;
break;
}
//Check for event caching the keys
console.table(this);
//Check for event if it catching the keys
//console.table(this);
}

};
Expand Down

0 comments on commit 5d392c8

Please sign in to comment.