Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rectangle x, y position
- var x = 400; // starting x position
- var y = 250; // starting y position
- var FPS = 60; // frames per second of the screen
- // physics variables:
- var velX = 0; // initial velocity at 0 (not moving)
- var velY = 0; // not moving
- var drag = 0.92; // drag force reduces velocity by 8% per frame
- var force = 0.35; // overall force applied to move the rectangle
- var angle = 0; // angle in which to move
- // called every frame (at 60 frames per second):
- function update(){
- // calculate distance between mouse and rectangle
- var dx = mouseX - x;
- var dy = mouseY - y;
- // calculate angle between mouse and rectangle
- var angle = Math.atan(dy/dx);
- if(dx < 0)
- angle += Math.PI;
- else if(dy < 0)
- angle += 2*Math.PI;
- // calculate the force (on or off, depending on user input)
- var curForce;
- if(keys[32]) // SPACE bar
- curForce = force; // if pressed, use 0.35 as force
- else
- curForce = 0; // otherwise, force is 0
- // increment velocty by the force, and scaled by drag for x and y
- velX += curForce * Math.cos(angle);
- velX *= drag;
- velY += curForce * Math.sin(angle);
- velY *= drag;
- // update x and y by their velocities
- x += velX;
- y += velY;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement