#### Archived

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

# projectiles

This topic is 6934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to have an arrow move from the player's position toward the enemy's. I'll know the x and y values of both at that point, so I just need each point that makes up the line to the enemy's position. I know I should know this, but I just can't seem to remember who to do it. I think it would have something to do with y=mx+b, maybe? Although, I can't remember how to get the slope (m) either...I'm forgetting all my algebra... Any help would be appreciated Edited by - Nazrix on 4/27/00 2:56:57 PM

##### Share on other sites
Well, you would have to calculate the slope even though you don''t like it. You could also use Brensemham''s (however you spell it ) algorithm and alter it to be able to use it with the projectile. y = mx + b is the slope formula (as you have pointed out). m is the rise over run, and b is the y-intercept. others will help fill in the gaps, since im on the run right now .

##### Share on other sites
y = mx+b is slope-intercept form (where m is the slope and b is a constant)

To find m, here''s the eq:

m = (y2-y1)/(x2-x1)

Actually, it really doesn''t matter which set of variables comes first as long as they line up (i.e., you can''t have y2 be first on the top and x1 first on the bottom)

If you code it, they will come...

Commander M
http://commanderm.8m.com
cmndrm@commanderm.8m.com

##### Share on other sites
Given two points (x1,y1) and (x2,y2) (I assume you are working in two dimensions here) you can find the points in between these two points as follows.

Using the parametric form of a line which is:
x = x1 + (x2-x1)t
y = y1 + (y2-y1)t
where t is a value between 0 and 1, which represents how far along the line you are.

For example, if you wanted to find the point exactly half way in between pt1 and pt2, you would use a value of 0.5 for t.

So if you wanted to shoot a bullet of at an enemy at (x2,y1), when you are at (x1,y1) just use the above equation, adding some value to t until it reaches 1.

PreManDrake

##### Share on other sites
Forgot to mention, if you are working in 3 dimensions the third part is:
z = z1 + (z2-z1)t

PreManDrake

##### Share on other sites
It''s also better to use parametric equations. If you use the y=mx+b formula, there is one special case you can''t do: vertical lines give m=1/0... which is a bad thing. You will notice that the parametric equations also turn nicely into vectors. Let A be the vector of the initial placement, B be the vector of the destination, and t be the same time, you end up with current position= A+(B-A)*t

(B-A) is the velocity vector.

##### Share on other sites
The parametric equation does look much better, but if I''m understanding this correctly, if I increase T by like .1 or so each time then the X and Y coordinates will have decimals in them. How would I round them up or down to a whole number? Is there a function for that already?

Thanks again

##### Share on other sites
As LaMonthe mentions in Tricks of the Windows Game Programming Gurus, there is an algorithm for doing this. It doesn''t exactly round anything up or down, but it finds the best fitting line using an error offset. This is normally used for vector games such as the original Asteroids. I didn''t exactly understand how he explained the algorithm, but maybe someone else can help you out.

"Remember, I'm the monkey, and you're the cheese grater. So no messing around."
-Grand Theft Auto, London

"It's not whether I win or lose, as long as I piss you off"
-Morrigan, Super Puzzle Fighter II Turbo

• 15
• 13
• 35
• 39