Jump to content

  • Log In with Google      Sign In   
  • Create Account


joe96

Member Since 31 Aug 2011
Offline Last Active Apr 21 2013 12:57 AM
-----

Posts I've Made

In Topic: Top - down 2d zombie game

05 September 2011 - 05:05 PM


I've been working on a top - down 2d zombie shooter. I've got all the player movement down but i have been having trouble with the shooting. I cant figure out how to make it shoot in the direction of where the mouse was clicked. I use java to program it. If anyone can help that would be great.


Well, you know where your player is on the screen (playerX and PlayerY), you know where the mouse was clicked (mouseX and mouseY), and you have a general velocity for the bullet (float BulletVelocity) right? You have 2 different methods you can use to set the bullet's X and Y velocity:
#1, The Trigonometry method (recommended), you set the position of the bullet at the player, and set velocity of the bullet to be:
// get the angle from the player to the mouse clicked position
float Angle = atan2(mouseX - playerX, mouseY - playerY);
VelocityX = cos(Angle)*BulletVelocity;
VelocityY = sin(Angle)*BulletVelocity;

Or, you can do it the conceptual/geometric method, by determining how far the mouse is from the player, and the time it should take for the bullet to get there:
// Get the distance of the mouse from the player (A^2 + B^2 = C^2)
float distanceX = mouseX - playerX;
float distanceY = mouseY - playerY;
float distance = sqrt(distanceX*DistanceX + DistanceY*DistanceY);

// get time it would take the bullet to get there travelling at BulletVelocity (assuming velocity is pixels/second)
float Time = distance/BulletVelocity;

// now give the X and Y speeds for the bullet to get to that point:
VelocityX = distanceX/Time;
VelocityY = distance/Time;



in the first example what is the atan2 supposed to mean. thanks for your reply.

In Topic: Randomly generated terrain

31 August 2011 - 11:07 PM

i use java and im kinda new to making games with it

PARTNERS