I’m trying to build a simple 2D game where I launch projectile with constant speed and then I have “gravity” areas that pull projectile to them (like gravity around planets or black holes).
I apply those ‘gravity’ forces in the following way in FixedUpdate():
var direction = GetGravityDirection(_projectileCollider);
_projectileCollider.GetComponent<Rigidbody2D>().AddForce(direction * Force, ForceMode2D.Impulse);
So every ‘black hole’ each fixed update apply some force to my projectile. I launch projectile like this (after I calculate direction and force of launch):
rigidBody2D.AddForce(direction * force, ForceMode2D.Impulse);
Now I want to calculate and build a trajectory. My plan is to calculate it step by step. I know initial position of projectile and force applied to it. I should be able to calculate positions for each fixedDeltaTime. I can’t find the initial velocity. From specs I understood that mode Impulse applies the force for one frame. Therefore I have force, time, mass and initial speed. I should be able to find the velocity of my projectile after first frame (which should be constant afterwards if no other forces are applied to the projectile). I’m trying to find that speed like this:
var gainedVelocity = force * Time.fixedDeltaTime / rigidBody2D.mass; // force is Vector3
None of the variables is 0, but I’m getting zero vector as the result (in fact I’m getting values close to zero). When I launch the projectile it logs some speed.
Can I solve this problem in the way I described? If yes, then what am I doing wrong?
Many thanks.
P.S. I can launch by setting velocity to my projectile, but I still don’t know if the approach I selected will work. Even if I deal with initial velocity, later I need to calculate the impact of each force from ‘black holes’ and I wanted to do this in the same way, by calculating it from F=ma formula and applying it to x1 = x0 + vt + 0.5at*t.