Jump to content

  • Log In with Google      Sign In   
  • Create Account

Thorion

Member Since 31 Jan 2012
Offline Last Active Feb 14 2012 08:44 PM

#4908710 2D Aiming accuracy

Posted by Thorion on 02 February 2012 - 08:07 AM

I managed to fix it myself. One thing I did was store X and Y coordinates as floats and only rounded off when actually drawing. My code needed some improvement too, here's what it looks like now:
private void aimShot() {
	  
		float distanceX = this.fX - this.paX;
		float distanceY = this.fY - this.paY;
	  
		boolean xIsNegative = false;
		boolean yIsNegative = false;
	  
		if (distanceX < 0) {
			xIsNegative = true;
			distanceX = -distanceX;
		}
	  
		if (distanceY < 0) {
			yIsNegative = true;
			distanceY = -distanceY;
		}
	  
		if (distanceX == 0) {
			distanceX = 1;
		}
	  
		if (distanceY == 0) {
			distanceY = 1;
		}

		float ratio = distanceX / distanceY;
	  
		this.speedY = 10 / (ratio + 1);
		this.speedX = ratio * this.speedY;
	  
		if (xIsNegative) {
			this.speedX = -this.speedX;
		}
	  
		if (yIsNegative) {
			this.speedY = -this.speedY;
		}
	}



PARTNERS