Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player {
- float maxSpeed = 0.1;
- float slowDown = 0.7;
- float acceleration = 0.2;
- float turnSpeed = 0.025;
- float mouseTurnSpeed = -turnSpeed/8;
- float moveSpeed = 0.1;
- float minWallDist = 0.2;
- float direction;
- float vdirection;
- PVector loc;
- PVector vel;
- Player (float xpos_, float ypos_, float direction_) {
- loc = new PVector(xpos_, ypos_);
- vel = new PVector(0, 0);//.rotate(direction_);
- direction = direction_;
- }
- void playerMovement() {
- float strafe = 0,mov = 0;
- PVector hitLocFwd = new PVector(0, 0), hitLocSides = new PVector(0, 0), hitLoc = new PVector(0, 0);
- if (!view) { //2D a/d turns player
- if (keys['a']) turn(turnSpeed);
- if (keys['d']) turn(-turnSpeed);
- } else { //3D a/d strafes player
- if (keys['a']) {
- strafe = moveSpeed;
- hitLocSides = hitLeft;
- } else if (keys['d']) {
- hitLocSides = hitRight;
- strafe = -moveSpeed;
- }
- if (mouseMoved) turn(mouseMove*mouseTurnSpeed);
- }
- if (keys['w']) {
- hitLocFwd = hitFwd;
- mov = 1;
- } else if (keys['s']) {
- hitLocFwd = hitRev;
- mov = -1;
- }
- //set hitLoc to most relevant intersection
- float dF = hitLocFwd.dist(loc);
- float dS = hitLocSides.dist(loc);
- if (dF > dS) hitLoc = hitLocSides;
- else hitLoc = hitLocFwd;
- // requested movement vector from controls
- PVector requestedMove = new PVector(strafe, mov*moveSpeed).rotate(-direction);
- //Collision detection
- PVector playerLoc = player1.loc.copy();//new PVector( player1.xpos, player1.ypos);
- float dist = (playerLoc.copy().add(vel)).dist(hitLoc);
- float distInv = 1-dist/minWallDist;
- if (dist < minWallDist) {
- requestedMove.mult(-distInv);
- vel.setMag(0);
- }
- // apply acceleration, velocity, deceleration
- vel.add(requestedMove.mult(acceleration));
- vel.limit(maxSpeed);
- if (requestedMove.magSq() == 0) vel.mult(slowDown);
- loc.add(vel);
- }
- int xcell() {
- return floor(loc.x);
- }
- int ycell() {
- return floor(loc.y);
- }
- float dxpos() {
- return loc.x-xcell();
- }
- float dypos() {
- return loc.y-ycell();
- }
- void turn (float val_) {
- direction = radianWrap(direction + val_);
- }
- void scanAngle (float val_) {
- vdirection = radianWrap(direction + val_);
- }
- float radianWrap(float k) {
- if (k > TWO_PI) k = k -TWO_PI;
- if (k < 0) k = TWO_PI + k;
- return k;
- }
- }
Add Comment
Please, Sign In to add comment