S
Wow, emmm, I will try it, but I'm not sure that I can use gravity.. The same de gun can shoot 45º(up-right), it can shoot -45º(bottom-right)Assuming you want a realistic looking trajectory, then you won't have a change in direction per second, but use gravity instead.
To do this we need to use best one equations of motion.
S = ut + 1/2(at^2)
Where S is displacement u is initial velocity, a is acceleration and t is time.
Let's break this down into two parts. : vertical and horizontal.
Let's start with horizontal.
We have no horizontal acceleration, so our equation becomes S = ut.
Our target displacement is X.x - G.x.
Our initial velocity is u × cos(theta).
So we have
X.x - G.x = ut × cos(theta).
Let's find t:
(X.x - G.x)/ u cos(theta) = t
Ok, so now we need can calculate the time!
Let's move on to vertical axis.
We know our vertical velocity is u × sin(theta), where U is initial speed, and theta is angle of the gun.
X is our target position. So X.y is our target height.
The gun is at position G. G.y is the guns height.
Our target displacement is X.y - G.y.
So, let's substitute those values into our equation.
X.y - G.y = u×sin(theta) + 1/2(at^2).
We know t from the horizontal axis, now we need tol solve for the acceleration a.
Let's rearage to find a
a = 2((X.y - G.y) - u sin(theta)) / t^2
And there you have it!
By first calculating the time using the simple horizontal axis, we can use it to determine the acceleration needed in the vertical axis.
Oh, is it more like a seeking missile than a projectile then?Wow, emmm, I will try it, but I'm not sure that I can use gravity.. The same de gun can shoot 45º(up-right), it can shoot -45º(bottom-right)
Yes, is in space also, so the can't be gravityOh, is it more like a seeking missile than a projectile then?
Ah, in that case, it's a totally different ordeal.Yes, is in space also, so the can't be gravity
I think that example will work for what I want, but I must have some way to calculate the time to collision.If it's a homing missile, I would probably take a look at this example by @YellowAfterlife.
You could quite easily measure the time as distance/speed. So get the distance to your target x and target y and divide it by the speed at which your missile is going.I think that example will work for what I want, but I must have some way to calculate the time to collision.
Yes, but I need that information on the CREATE of the object, and the direction of the bullet is not straight so distance/speed doesn't workYou could quite easily measure the time as distance/speed. So get the distance to your target x and target y and divide it by the speed at which your missile is going.
couldn't you simulate the flight via a while (true) or do-until loop and measure how many iterations it took to get to the target?Yes, but I need that information on the CREATE of the object, and the direction of the bullet is not straight so distance/speed doesn't work
You have a point, if you shot in the opposite direction of the enemy then the distance is less, but the distance is going to become more after it when it launches.Yes, but I need that information on the CREATE of the object, and the direction of the bullet is not straight so distance/speed doesn't work
You have a point, if you shot in the opposite direction of the enemy then the distance is less, but the distance is going to become more after it when it launches.
You would have to predict the path somehow and measure it according to that...
The target is not movingcouldn't you simulate the flight via a while (true) or do-until loop and measure how many iterations it took to get to the target?
var _projectile_speed = 8; //any value should work (larger values will result in some loss of accuracy if projectile turns in discrete steps
var _A = angle_difference(point_direction( x, y, target.x, target.y ),image_angle);
var _d = point_distance( x, y, target.x, target.y );
var _B = 90 - _A;
var _C = 180 - 2 * _B;
var _radius = _d / 2 / dcos( _B );
var _circumference = 2 * pi * _radius;
var _arc_length = _C / 360 * _circumference;
var _time = _arc_length / _projectile_speed;
var _turn_rate = _C / _time;
with (instance_create(x,y,obj_projectile)) {
turn_rate = _turn_rate; //add to direction each step.
lifetime = _time; //in case you want projectile to know how long until it reaches the point it was fired at
speed = _projectile_speed;
direction = other.image_angle;
}
Nice diagram, Good code! Do you mind walking us trough it?If your projectile's only constraint is their speed, and you want them to turn at a constant rate until they hit a stationary target, then you can get almost exact solutions like this.
This is pretty different from the behavoir you'd probably like in a homing missile though. Homing missiles are pretty interesting. Proportional, (or constant bearing) navigation gives very good results.
View attachment 17236Code:var _projectile_speed = 8; //any value should work (larger values will result in some loss of accuracy if projectile turns in discrete steps var _A = angle_difference(point_direction( x, y, target.x, target.y ),image_angle); var _d = point_distance( x, y, target.x, target.y ); var _B = 90 - _A; var _C = 180 - 2 * _B; var _radius = _d / 2 / dcos( _B ); //r in diagram below var _circumference = 2 * pi * _radius; var _arc_length = _C / 360 * _circumference; var _time = _arc_length / _projectile_speed; var _turn_rate = _C / _time; with (instance_create(x,y,obj_projectile)) { turn_rate = _turn_rate; //add to direction each step. lifetime = _time; //in case you want projectile to know how long until it reaches the point it was fired at speed = _projectile_speed; direction = other.image_angle; }