Advertisement
heroofhyla

Movement System

May 21st, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package com.aezart.gamebase;
  2.  
  3. public class MovementSystem {
  4.    
  5.     public void tick(MotionComponent m, PositionComponent p){
  6.         m.xSpeed += m.xAcceleration;
  7.         m.ySpeed += m.yAcceleration;
  8.         m.direction = Math.atan2(m.ySpeed, m.xSpeed);
  9.         if (m.friction != 0){
  10.             double speed = Math.sqrt(m.xSpeed * m.xSpeed + m.ySpeed * m.ySpeed);
  11.             speed -= m.friction;
  12.             if (speed < 0){
  13.                 speed = 0;
  14.             }
  15.             if (speed > m.maxSpeed){
  16.                 speed = m.maxSpeed;
  17.             }
  18.             m.xSpeed = Math.cos(m.direction) * speed;
  19.             m.ySpeed = Math.sin(m.direction) * speed;
  20.             m.speedSqr = speed * speed;
  21.         }else{
  22.             m.speedSqr = m.xSpeed * m.xSpeed + m.ySpeed * m.ySpeed;
  23.             if (m.speedSqr > m.maxSpeed * m.maxSpeed){
  24.                 double speed = m.maxSpeed;
  25.                 m.xSpeed = Math.cos(m.direction) * speed;
  26.                 m.ySpeed = Math.sin(m.direction) * speed;
  27.                 m.speedSqr = speed * speed;
  28.             }
  29.         }
  30.        
  31.         p.xPos += m.xSpeed;
  32.         p.yPos += m.ySpeed;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement