There nothing wrong with my code really, I was watching a tutorial to help me learnt to makegames in canvas. ive understood every single bit of it but the one thing i dont understand is why must I add velocity to my equation? i understand its the speed and direction of an objet but why must iI add it? ive been trying to understand it for 8 hours now. and i dont think i can continue with learning until I understand why. soeone please help me…
Ive interrogated 2 seperate AI bots but what they were saying were making no sense to me whatsoever even when i asked them to simplify it.
here is my code of course:
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
canvasWidth = 770;
canvasHeight = 650;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
let gravity = 7.4;
class Player{
constructor(){
this.position = {
x: 175,
y: 0
}
this.velocity = {
x: 0,
y: 0
}
this.width = 65;
this.height = 60;
}
draw(){
ctx.fillStyle = "goldenrod";
ctx.fillRect(this.position.x, this.position.y, this.width, this.height)
}
update(){
this.draw();
this.position.y += this.velocity.y;
if(this.position.y + this.height + this.velocity.y <= canvas.height){
this.velocity.y += gravity;
}else{this.velocity.y = 0}
}
}
let player = new Player();
player.draw()
function animate(){
requestAnimationFrame(animate)
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
player.update();
}
animate()
the specific part of the code i dont understand is :
this.position.y + this.height + this.velocity.y <= canvas.height
i understand that this.position.y + this.height is the position of the bottom of the player but if i already have the bottom position of th eplayer why do i have to add velocity?
im only 15 and i just started taking geometry lol