Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


2D Aiming accuracy


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Thorion   Members   -  Reputation: 102

Like
0Likes
Like

Posted 31 January 2012 - 12:22 AM

Hello,

I am making a 2D top down shooter and am struggling with aimed shots by enemies. At the moment it works OK but it's not very accurate. Here is my code: (startX,startY,playerX,playerY are class variables). Can anybody help me make this more accurate? Math's isn't my best subject!

private void aimShot() {
		this.setLaserX(startX);
		this.setLaserY(startY);
	  
		float distanceX = startX - playerX;
		float distanceY = startY - playerY;
	  
		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;
		float speedX, speedY;
	  
		if (distanceX > distanceY) {
			ratio = distanceY/distanceX;
			speedX = 10*(1-ratio);
			speedY = 10*ratio;
		} else {
			ratio = distanceX/distanceY;
			speedY = 10*ratio;
			speedX = 10*(1-ratio);
		}
	  
		if (xIsNegative) {
			speedX = -speedX;
		}
	  
		if (yIsNegative) {
			speedY = -speedY;
		}
	  
		intSpeedX = Math.round(speedX);
		intSpeedY = Math.round(speedY);
	  
	}


Sponsor:

#2 Thorion   Members   -  Reputation: 102

Like
1Likes
Like

Posted 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;
		}
	}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS