🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

2d physics

Started by
2 comments, last by Qbot 23 years ago
well, i''ve been working on this problem, and my friend found a limited solution. Anyways, here it is.. let''s say you are a turret. there is a car driving, with and arbitrary velocity dx, and dy, from point cx, cy given the speed of your bullets, v, what angle(s) would produce a collision at a later time? assume the turret is at 0,0 time for some ascii art: o= __(direction of travel) |/ c (location of car at t = 0) my friend found a solution if the car is driving parallel to the turret, and i know i could apply the rotation formula, but when i coded his solution it didn''t work. I think the best way to try is to make an equation for d, the distance between the car and turret. I think the quadratic fromula is needed, because i figure there can be two solutions.. The worst part is, i found a site with the solution before i knew i needed this problem solved. so please help me! and good luck! -Qbot
Advertisement
The problem is actually a lot easier than you might think.

All you need to know is the position of the car and its velocity relative to the turret, then convert these into polar coordinates . We assume that the turret is at the origin of the coordinate system. So, the position at time t is and the velocity is . You can now convert this into a new position and new velocity using the following:

x = r.cos(theta)
y = r.sin(theta)

since r = sqrt(x*x + y*y) then

theta = arccos(x/r) = arcsin(y/r)

Secondly, you can convert velocity according to:

dr/dt = cos(theta)*(dx/dt) + sin(theta)*(dy/dt)

dtheta/dt = ((-sin(theta))/r)*(dx/dt) + ((cos(theta))/r)*(dy/dt)

So, you can then easily compute the starting radius and angle to the car and update this at each time step.

REMEMBER THOUGH: The velocities and positions are measured relative to the turret!

Good luck,

Tim
Or you could define the trajectory of your bullets as parametric line equations where the parameter represents time. Given a line in parametric form, we can easily perform an intersection test with the objects which might be hit. These objects could themselves be trajectory paths. Solving the intersection and substituting the cooridnates of the intersection back into the parametric equation yields the parameter t, or the time of intersection. If the t''s of both trajectories are reasonably close, than you have a collision.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Another way to do this that is by quadratics is fairly simple, sorry I took so long but I had to work it out from first principles.

Imagine you have two velocities that make up the final velocity of the bullet. One velocity matches speed with the car, the second velocity is in the direction of the car and is the velocity which actually moves the bullet towards the car. The combined value of these velocities is the speed of the bullet, so you have

C - the velocity of the car
B - the velocity of the bullet
V - a normalised vector (i.e. with a magnitude of 1) in the direction from the turret to the car.

So you have to resolve the following equation to find a value k

B = C + kV

k is just a multiplier which tells you how much velocity you can put in the direction of the car.

To split the equation up into x and y values you get a vector for the velocity of the bullet that looks like

B = (Cx + kVx, Cy + kVy)

As B is a magnitude you can get the magnitude of the vector by

B^2 = (Cx + kVx)^2 + (Cy + kVy)^2

B^2 = Cx^2 + k^2Vx^2 + 2CxkVx + Cy^2 + k^2Vy^2 + 2CykVy

Looks hard? Only because they''re letters not values, you end up with quadratic equation

k^2(Vx^2 + Vy^2) + k(2CxVx + 2CyVy) + (Cx^2 + Cy^2 - B^2) = 0

In a quadratic equation you have

ak^2 + bk + c = 0

so

a = (Vx^2 + Vy^2)
b = (2CxVx + 2CyVy)
c = (Cx^2 + Cy^2 - B^2)

to solve to find k you use

k = (-b +/- sqrt(b^2 - 4ac)) / 2a

You then have two values of k. If you calculate the bullet velocity for those two values of k you can use that to work out the point in time when the bullet would hit the car by

Position(Car) + (Velocity(Car) * time) = Position Turret + (Velocity(Bullet) * time)

So

time = (Position(Car) - Position(Turret)) / (Velocity(Bullet) - Velocity(Car))

one of the times will come out negative, use the bullet velocity that gives the positive one.

Okay, reading this you might want to do it one of the other ways, but this involves no trigonometry and is fairly simple algorithmically, just long winded.

Mike

This topic is closed to new replies.

Advertisement