1

i want to use an acceleration algorithm to change the speed of a supposed aicraft to move it in a 2D environment. In example:

positionX = positionX + (speed based on acceleration);
positionY = positionY + (speed based on acceleration);

My problem is that if i do it like that the result, assuming the speed is 50, will be position += 50, which is entirely wrong since i don't want to use speed as the number of X it will move on the axis. I want the speed to be some sort of basis for the axis numbers. In example, if lets say speed is 50 and 50 speed means 3 X per movement then that means

positionX + speed = positionX+3;

I want to create that in code along with an acceleration method that will increase the speed by a percentage. So my question is how to make speed as a reference point kind of thing.

user3498565
  • 7
  • 1
  • 7
  • you need speed and direction then, or just simplified this and have two speex, one for x axis and another for Y axis – user902383 Apr 04 '14 at 14:44
  • i want the speed to be the same i already have a direction method but i want to change the axis position based on speed. i have implemented all this but with simple x+3 or y+3 and i want to implement speed now – user3498565 Apr 04 '14 at 14:50
  • I would use discrete time steps and in every step: 1. Calculate acceleration value if it is changing; 2. Add acceleration to speed; 3. Convert speed into increment of coordinate (as I understand you are using different units for speed and coordinate); 4. Increment the coordinates by value retrieved in step 3. BTW you possibly want also to count for the angle of the speed vector, as you have two coordinates and increasing them both by value of speed would yield speed that is sqrt(2) times bigger than the actual (Pythagoras theorem). – Kristjan Veskimäe Apr 04 '14 at 14:51
  • now if only i could implement that in code would be great. also to be honest i would appreciate it if you could help with that since i have no clue for math algorithms – user3498565 Apr 04 '14 at 15:21

3 Answers3

3

Keep it simple. The physics aren't difficult. The "math" is no more difficult than multiplying and adding.

You want to deal with changes in velocity and position over increments of time.

Position, velocity, and acceleration are vector quantities. In your 2D world, that means that each one has a component in the x- and y-directions.

So if you increment your time:

t1 = t0 + dt

Your position will change like this if the velocity is constant over that time increment dt:

(ux1, uy1) = (ux0, uy0) + (vx0*dt, vy0*dt)

The velocity will change like this if the acceleration is constant over that time increment dt:

(vx1, vy1) = (vx0, vy0) + (ax0*dt, ay0*dt)

Update your accelerations if there are forces involved using Newton's law:

(ax0, ay0) = (fx0/m, fy0/m) 

where m is the mass of the body.

Update the positions, velocities, and accelerations at the end of the time step and repeat.

This assumes that using the values for acceleration and velocity at the start of the step is accurate enough. This will limit you to relatively smaller time steps. (It's called "explicit integration".)

Here's an example. You have a cannon at (x, y) = (0, 0) with a cannonball of mass 20 lbm inside it. The cannon is inclined up from the horizontal at 30 degrees. We'll neglect air resistance, so there's no force in the x-direction acting on the cannonball. Only gravity (-32.2 ft/sec^2) will act in the y-direction.

When the cannon goes off, it'll launch the cannonball with an initial speed of 40 ft/sec. The (vx, vy) components are (40*cos(30 degrees), 40*sin(30 degrees)) = (34.64 ft/sec, 20 ft/sec)

So if you plug into our equations for a time step of 0.1 second:

(ax0, ay0) = (0, -32.2 ft/sec^2)
(vx1, vy1) = (vx0, vy0) + (ax0, ay0)*dt = (34.64 ft/sec, 20 ft/sec) + (0, -3.22 ft/sec) =  (34.64, 16.78)
(ux1, uy1) = (ux0, uy0) + (vx0, vy0)*dt = (3.464 ft, 1.678 ft)

Take another time step of 0.1 seconds with these values as the start. Rinse, repeat....

You do this individually for both x- and y- axes.

You can make this slightly more real by making the initial height of the cannon ball equal to half the diameter of your cannon wheels.

You can add a small negative acceleration in the x-direction to simulate wind resistance.

Assume your target is off to the right along the x-axis.

If you fire with the cannon pointing straight up the equations will show the ball going up, slowing down under it reaches its apex, and then coming straight down. No hit, except perhaps on your head and the cannon.

If you fire with the cannon horizontal, the equations say the ball with move with constant velocity in the x-direction and only fall the initial height of the cannon. Your enemies will taunt you: "Air ball! Air ball!"

So if you want the ball to intersect with the ground (a.k.a. reach position y = 0) within some blast radius of your target's location, you'll have to play with the initial speed and the angle of the cannon from the horizontal.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • well damn this is s very simple explanation, thanks. I care not for mass so we can ignore that. Now just to make sure, to do this in java i basically have to do it individually for both X and Y axis right? And also is it possible to tell me how to find my distance from my own (x,y) to the target's (x,y)? i know that (myX-targetX)+(myY-targetY) is wrong and as far as i remember i need to do something with the corners of the straight line connecting the 2 spots but i can't really do that in java. – user3498565 Apr 04 '14 at 23:59
1

You just need to use the equations of movement for each axis:

x(t)= x0 + v0*t + 1/2*a*t^2 #where x0 is the initial position, v0 is the initial velocity and a is the acceleration you are considering, all relatively to an axis

Now you need to define the instants at which you are calculating the position, as well as writing the values of velocity and acceleration in the correct units as @markusw suggested.

Mary
  • 153
  • 2
  • 2
  • 9
  • that's what i want to use my problem is converting this in java code. being completely honest i suck at math so this is what i need help in. – user3498565 Apr 04 '14 at 15:11
  • @user3498565: You need no *more* math, it's all there. Just don't forget to use a `double` 0.5 instead of integer division and rewrite `t` squared as `t*t`. Then it's valid Java. – maaartinus Apr 04 '14 at 15:51
-1

like this:

double calculateSpeed(double value) {
  return value / 16.66;
}

And call it like this:

positionX = positionX + calculateSpeed(50);
markusw
  • 1,975
  • 16
  • 28