This topic is now archived and is closed to further replies.


2D Projectile

Recommended Posts

This has been gone over many times, but I cant figure out how to make it work. I have my characters position (CharX, CharY) and the mouse position (MouseX, MouseY). Now I need to sent a projectile from the character through where the mouse clicked and off the screen. The character is in the middle of the screen and the mouse can get clicked anywhere on the screen. I dont need gravity, wind resistance, I just need to adjust the projectile speed. Can anyone help with this? -Thanks

Share this post

Link to post
Share on other sites
You already have another thread open with the same question which is not a very good idea.
Nevertheless (and to stop you from opening further threads):
What you are looking for is the velocity v, not the speed s (speed s will be chosen by you):
v = s * Normalize(MouseX-CharX, MouseY-CharY)
Position of projectile P:
P = CharXY + v * time.

Share this post

Link to post
Share on other sites
for a ''real'' projectile, which is under a constant gravity, to hit a target point from a position,

// get the elevation angle required to hit a target with a

// projectile influenced by gravity, and with an initial velocity

// if the target can''t be reached, returns false

bool CalculateRequiredElevationAngle(float &angle, float dx, float dy, float vel, float gravity)
float dx2 = dx*dx;

float lambda = (gravity * dx2)/(2.0f * vel*vel);

if (lambda < 0.0f)
return false;

float disc = dx2 - 4.0f * lambda * (-dy + lambda);

if (disc < 0.0f)
return false;

float tang = (dx - (float) sqrt(disc)) / (2.0f*lambda);

angle = (float) atan2(tang, 1.0f);

return true;

I know it''s not exactly what you are looking for, but it''s difficult, as both are inter-dependent. so what i''d suggest is, use the maximum velocity your projectile is allowed to use, and do a binary search until you have a staisfying velocity, and a good angle. Most of the time, the muzzle velocity is rather cosntant, you just need to elevate the gun so that teh projectile hits its designated target.

Share this post

Link to post
Share on other sites
So you''re working on a 2D game and you want to shoot a bullet from your character in the direction of where your mouse pointer sits? A ''just plain linear'' sort of bullet movement? Straight from point A to point B? Well, I''m still pretty new to game programming, and I only know C and CPP (hopefully you didn''t want a VisualBasic example!), but here''s what I''m using in my sidescroller game to get enemy bullets to shoot toward my ship:
			float xspd, yspd, avgSpeed;

xspd = MyShip.xloc - EnemyShip.xloc;
yspd = MyShip.yloc - EnemyShip.yloc;

avgSpeed = (float)fabs(xspd) + (float)fabs(yspd);

xspd = ((xspd / avgSpeed) * 4);
yspd = ((yspd / avgSpeed) * 4);

Bullet.xspeed = xspd;
Bullet.yspeed = yspd;

The above code will fire a projectile from the enemy ship toward my ship, and the bullet will scroll off the screen if I manage to dodge it. You should be able to replace ''MyShip.xloc'' and ''MyShip.yloc'' with your mouse''s x/y coords, then replace ''EnemyShip.xloc'' and ''EnemyShip.yloc'' with your shooter''s x/y coords. My formula isn''t perfect (at certain angles, the bullet seems to fire a little faster than if fired from a different angle), but hopefully it''ll give you a starting point.

Pretty basic stuff, but I''m no math guru so it''s all I can do. If you''re interested, here''s what''s happening:
- First, store the difference between the ''target'' (MyShip) and ''shooter'' (EnemyShip) x/y locations into ''xspd'' and ''yspd''.
- Store the absolute value sum of those two variables in ''avgSpeed''. It has to be a positive number, and both variables have to be temporarily made positive (hence the ''fabs'' command) to get this value. For example, if ''xspd'' is -40 and ''yspd'' is 80, ''avgSpeed'' will be 120 ( not 40! ). After this calculation is done, ''xspd'' will still be -40 and ''yspd'' will still be 80.
- Store the value created by dividing ''xspd'' into ''avgSpeed'' (this will be your projectile''s ''direction''). Do the same for ''yspd''.
- Multiply that new value by your projectile''s speed value (in my example, the projectile''s speed is a constant 4).
- Store the new ''xspd'' and ''yspd'' values into your projectile''s speed info.
- Finally, spawn the bullet where the ''xspd'' and ''yspd'' values were taken (if you spawn the bullet in front of the shooter after you take your xspd/yspd readings, then the bullet will miss the target by that far).

Good luck!

Share this post

Link to post
Share on other sites