Jump to content

vessel.SetPosition() issue :)


Recommended Posts

Greetings. Just wanna say sorry for my english, i hope you'll understand my question from code i posted below...

I've found a bug and looking for help to overcome it:

vessel.SetPosition(vessel.GetWorldPos3D());

sets the vessel a little bit behid from previous position.

For example i want my vessel to stay in current position:

public override void OnFixedUpdate()
{
base.OnFixedUpdate();

vessel.SetPosition(vessel.GetWorldPos3D());
}

but it starts flying backwards ( in vessel.transform.down direction) lol :)

This is my attempt to catch delta bug:

public override void OnFixedUpdate()
{
base.OnFixedUpdate();

bug = Vector3d.Distance(vessel.GetWorldPos3D(), prevPos); //here is it!
vessel.SetPosition(vessel.GetWorldPos3D(); //+ (vessel.transform.up * bug)); this will work every secon'd time
prevPos = vessel.GetWorldPos3D();

}

The interesting thing is that "bug" is changing with speed, ship mass, and direction.

Any ideas?

Link to comment
Share on other sites

Ah, solution always come after posting:

public override void OnFixedUpdate()
{
base.OnFixedUpdate();

bug += vessel.GetWorldPos3D().magnitude - prevPos.magnitude;
vessel.SetPosition(vessel.GetWorldPos3D() + (vessel.transform.up*(float)bug));
prevPos = vessel.GetWorldPos3D();
}

this works, but only in surface.

In orbit this is laggy as hell.

Edited by EvilKot
Link to comment
Share on other sites

For example i want my vessel to stay in current position

That's almost certainly not the right way of doing whatever you're trying to do.

Hackness aside, I can think of a few possible reasons for this behaviour:

1. Different definition of position. Vessel represents a collection of parts, so how exactly do you define it's position? Even without getting into the detail of unity structure there are several options - root component, current control location, center of mass, geometric center. Have you tried the different functions giving the vessel's position (e.g. vessel.transform.position) and seeing whether they show the same behaviour.

2. Your code may be running before the physics and/or other classes (orbit), so if their are forces acting on the vessel that frame then they'll more them slightly from the position you specify. Ditto for the vessel.GetWorldPos3D() if it returns a pre-computed value.

3. GetWorldPos may be designed for more approximate duties and is less accurate then SetPos, hence your deviation.

P.S. I wouldn't call base.OnFixedUpdate() or similar methods.

Link to comment
Share on other sites

Don't get too hung up on Vessel, it seems to be a class used to represent data about a collection of physics objects (part rigidbodies joined by Joints) and not an entity itself. Any methods it has that apply to physics matters are almost certainly convenience methods that loop through all the parts and issue commands to each of them.

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...