• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VermiVermi · Jan 10, 2017 at 09:43 PM · 2d2d-physicstrajectory

Building trajectory in 2D, getting initial velocity after ForceMode2D.Impulse was applied

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=m*a formula and applying it to x1 = x0 + v*t + 0.5*a*t*t.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scoutas · Jan 10, 2017 at 10:47 PM 0
Share

It might be a better idea to do the application for the force with Impulse, inside of a method that you would call once.

And okay, maybe I'm just a little tired, but I'm having a bit of a difficulty following. What is it exactly what you're trying to do? You want to build a trajectory (I assume you want to draw it out as well, right?), and you want the trajectory to be there since the second of the launch and for it to take in account the added force afterwards, right?

By the way, does the object movement work the way you want it to work? It'd be great if you described it a little as well.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VermiVermi · Jan 11, 2017 at 08:57 AM

I changed starting Force to just velocity for my projectile, but I still have ForceMode2D.Impulse in 'black holes'. So I have something like this for trajectory:

 public void BuildNewPath(Vector3 initialPosition, Vector3 initialVelocity, List<Gravity> affectors, float projectileMass)
 {
     _lineRenderer.SetVertexCount(0);
     int stepsCount = 1000;
     float timeDelta = Time.fixedDeltaTime;

     _lineRenderer.SetVertexCount(stepsCount);
     Vector3 position = initialPosition;
     Vector3 velocity = initialVelocity;
     for (int i = 0; i < stepsCount; ++i)
     {
         _lineRenderer.SetPosition(i, position);

         var acceleration = CalculateResultingAcceleration(position, affectors, projectileMass);

         position += velocity * timeDelta + 0.5f * acceleration * timeDelta * timeDelta;
         velocity += acceleration * timeDelta;
     }
 }

 private Vector3 CalculateResultingAcceleration(Vector3 position, List<Gravity> affectors, float projectileMass)
 {
     Vector3 resultingAcceleration = Vector3.zero;
     foreach (Gravity affector in affectors)
     {
         resultingAcceleration += affector.GetForce(position) / projectileMass;
     }
     return resultingAcceleration;
 }

But it doesn't seem to work the way I expected. Should I debug this approach or change 'black holes' to change velocity of my projectile in Fixed update instead of applying force? I used forces initially as I wanted to have some kind of non-linear gravity forces, they would change based on distance from projectile to center of each 'black hole'. I can change this to velocities, but I still don't know if that is a valid approach to the problem I'm trying to solve.

Thanks

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Ball Kicking, similar to "Sports Heads", or "1on1 Soccer" -1 Answers

Why didnt my Collider works? 0 Answers

Tweaking the Trajectory Simulation from the wiki 0 Answers

Using animated transform to push object away? 2D 0 Answers

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges