Jump to content

Applying a Force to center of mass of a Vessel


Recommended Posts

Applying a force to a part is easy

Rigidbody.AddForce(part.vessel.velocityD.normalized, ForceMode.Force)

However, if the force isn't direct directly in line of the vessel, it is pulled into  one direction and starts spinning

Therefore the question is, how to apply a force to the center of mass of a vessel?

Edited by FreeThinker
Link to post
Share on other sites

I think that would only pull it in the direction in the center of mass at all times

I instead want to push it in the opposite direction it is heading.

What I need is to get access of the RigitBody of the vessel itself instead of a specific part

 

Link to post
Share on other sites
19 minutes ago, FreeThinker said:

What I need is to get access of the RigitBody of the vessel itself instead of a specific part

There is no such thing; a vessel is just a collection of parts. You could apply an acceleration by manipulating the velocity, and obtain the magnitude of that acceleration from Newton's second, but there's no central RigidBody, as far as I know.

Link to post
Share on other sites

Well I think I have found a partial solution, which is to use the RegitBody of the vessel

var vesselRegitBody = part.vessel.GetComponent<Rigidbody>();
vesselRegitBody.AddForce(part.vessel.velocityD.normalized * -(float)effectiveOrbitalVesselDragInKiloNewton, ForceMode.Force);

However this still does not apply a force directly to its center of mass

Edited by FreeThinker
Link to post
Share on other sites

In an attempt to compensate, I tried

vesselRegitBody.AddForceAtPosition(part.vessel.velocityD.normalized * -(float)effectiveOrbitalVesselDragInKiloNewton, vesselRegitBody.centerOfMass, ForceMode.Force);

But this doesn't appear to have any positive effect

Why is this so hard while the engine has to have access to this variable to apply gravity?

Edited by FreeThinker
Link to post
Share on other sites
10 hours ago, Benjamin Kerman said:

@FreeThinker just making sure: velocityD is a Vector3d, and AddForceAtPosition requires a Vector3 (Unity). 

Isn't the Vector3d simply a double precision version of Vector3 which uses floats? It probably contain an auto cast that when you use it with  AddForceAtPosition it is automatically  converted to Vector3

Link to post
Share on other sites

Yes, but most of the time when you call a method that requires a Vector3 with a Vector3D you are not using the proper coordinate system.

Edited by sarbian
Link to post
Share on other sites

KSP has no physics concept of the entire vessel, it's really just a a bunch of parts bound together by physics forces.

I think what you want to do is apply the force to each part proportionally to its mass.

BTW, part.vessel.GetComponent<Rigidbody>() will return the root part's rigidbody (vessel is attached to the same GameObject as the root part)

Link to post
Share on other sites
  • 3 weeks later...
On 11/29/2017 at 7:03 AM, blowfish said:

KSP has no physics concept of the entire vessel, it's really just a a bunch of parts bound together by physics forces.

I think what you want to do is apply the force to each part proportionally to its mass.

I tried

                foreach (Part currentPart in part.vessel.Parts)
                {
                    currentPart.AddForce(part.vessel.velocityD.normalized * -dEffectiveOrbitalVesselDragInNewton * 1e-3 * (part.mass / totalMass));
                }

But it does not generate enough force. Possibly not all force is applied to the correct direction. I guess I need to compensate for this somehow. I guess I have to rotate the part axis in the direction of the axis of the vessel.

Edited by FreeThinker
Link to post
Share on other sites
On 11/26/2017 at 12:15 PM, sarbian said:

Yes, but most of the time when you call a method that requires a Vector3 with a Vector3D you are not using the proper coordinate system.

Alright, How to convert from Vector3D to Vector3 coordinate system?

Edit: I guess this should work:

var partHeading = new Vector3d(currentPart.transform.up.x, currentPart.transform.up.y, currentPart.transform.up.z);

 

Edited by FreeThinker
Link to post
Share on other sites

For unknown reason the method call

Vector3d.RotateTowards(partHeading.normalized, part.vessel.velocityD.normalized, (float)Math.PI, 1);

result in a exception stating "MissingMethodException: Cannot find the requested method" anyone know what is needed?

 

Edited by FreeThinker
Link to post
Share on other sites
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...