Okay, it's not showing me an edit button and I CBA to reload, so I'll just make a new post.
Since I don't know C# I'll show you a rough Ruby version of the class to demonstrate the technique.
class Bullet
def initialize(start_x, start_y, dest_x, dest_y, velocity)
@start_x = start_x
@start_y = start_y
@dist_x = dest_x - start_x
@dist_y = dest_y - start_y
distance = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y)).to_i
@total_steps = distance / velocity #assuming velocity is measured in distance per frame
@curstep = 0
@x = @start_x
@y = @start_y
end
def update
if @curstep == @total_steps
explode
else
@x = @start_x + ((@dist_x * @curstep) / @total_steps)
@y = @start_y + ((@dist_y * @curstep) / @total_steps)
@curstep += 1
end
end
def draw
#draw the bullet (no movement should be processed here)
end
endOf course that's pretty rough example, but it should demonstrate the technique. Calculate your distance in the ctor and use that distance to determine the number of frames it will take. (If velocity is distance-per-frame then total frames will be distance / velocity.)
When you update your position the math is:
(total distance) * (current step / total steps)
but since you're using integers you want to make sure that the multiplication takes place before the division (to prevent loss of precision).
For instance, to calculate half of 4 mathematically you'd say:
4 * (1 / 2)
But in programming, if you're using integers then you'd get a result of zero from that because (1 / 2) rounds down to zero. So instead you'd say:
(4 * 1) / 2
Which would evaluate to 2.
Generally when you're mixing multiplications and divisions you should try to do the divisions last since division reduces precision.
Because of this our position calculation becomes:
(total distance * current step) / (total steps)
You do that calculate the x and y positions separately this way for every frame. The idea is that (current step / total steps) represents the fraction of the total motion that you've reached. Multiplying that fraction by the total distance you need to move gives you the correct position for that frame. When current steps becomes equal to total steps then you've reached the final frame and you can explode the bullet. This technique is called "interpolation". The start and end are the "poles" and you're calculating an inter-pole position. This is a useful technique used in several ways in graphics programming.
You may want to take a Physics 100 course if you're planning on making games. A 100 course is usually pretty easy and basic physics deals a lot with this kind of calculation.
I'm not familiar with the C# syntax, but in your ctor could you test the value of 'Form1.Base.xfire' by printing it out or looking at it in the debugger and see if it's the correct value? I see you're using Visual Studio. VS has a very nice debugger. It can really help out with problems like this. You may want to spend a few minutes to familiarize yourself with its use.
Hope that helps!
Edited by Khatharr, 21 May 2012 - 09:28 PM.