Jump to content

Decomposing Velocity to get velocity projected on another vector


Recommended Posts

I have two vessels nearby. Vessel A is moving towards Vessel B, but not directly at it. I want to decompose the velocity vector into two vectors, such that V = M + N where M is the vector pointing directly from A to B. I have a point on vessel B "magnetCenter" below and "attractedVessel" is the Vessel A.

I get a vector pointing from A to B by subtracting locations "B - A"


(magnetCenter - attractedVessel.findLocalCenterOfMass()).normalized

I then project the velocity onto that vector to get the component of the velocity pointing from A to B.




Vector3 velocityComponentTowardsMagnet =
Vector3.Project(attractedVessel.rb_velocity, (magnetCenter - attractedVessel.findLocalCenterOfMass()).normalized );


printIntermittantFormatted("rel V: {0:#.00}, rb_V: {1:#.00}", velocityComponentTowardsMagnet.magnitude, attractedVessel.rb_velocity.magnitude);


As expected the projected result's magnitude is smaller than original velocity, but when Vessel A is a good distance away and moving directly towards Vessel B, then I would expect the magnitude of the velocity and the magnitude of the projection to be very close. But I get a big difference such as "rel V: .74, rb_V: 4.36".

Very similar result from using vessel.rigidBody.velocity instead of vessel.rb_velocity. The two are very close but are not identical(when referenced in the same fixedUpdate)


Vector3 velocityComponentTowardsMagnet2 =
Vector3.Project(attractedVessel.rigidbody.velocity, (magnetCenter - attractedVessel.findLocalCenterOfMass()).normalized );

printIntermittantFormatted("rel V: {0:#.00}, V: {1:#.00}", velocityComponentTowardsMagnet2.magnitude, attractedVessel.rigidbody.velocity.magnitude);

I also tried this but it seemed to give me non-sense I think because it is not the relative velocity of the center of mass and it also incorporates angular velocity:

var relativeVelocity = attractedVessel.rigidbody.GetRelativePointVelocity(magnetCenter);

I have a feeling it's because I'm not accounting for the velocity of vessel B. I really need to get the true relative velocity and then do the projection on that. Such as the relative velocity when you've targeted one vessel.

Link to comment
Share on other sites

Try using orbital velocity instead of the rigidbody velocities. May not help, but at the very least it'll be more consistent (KSP doesn't use a fixed physics reference frame position/velocity and using a velocity that isn't against a known reference frame sounds fishy to me). My suspicion is that the velocity of the active vessel is going to be non-zero and you aren't accounting for that with attractedVessel.rigidbody.velocity.magnitude

Target relative velocity

FlightGlobals.ActiveTarget.obt_velocity - FlightGlobals.ActiveVessel.obt_velocity

Edited by Crzyrndm
Link to comment
Share on other sites

Thanks, helped me at least know that I was on the right track.

This was the culprit because somehow I accidentally called findLocalCenterOfMass instead of World:

magnetCenter - attractedVessel.findLocalCenterOfMass()

Link to comment
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...