Porting from 3.xx to 4.00 - need help

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Mon Jun 19, 2023 2:31 pm

that that's incorrect, it should be a point in global space common to both

try this:
Code: Select all
ndMatrix matrix (ndGetIndetityMatrix());
matrix.m_poist =  ((pMainBody->GetMatrix().m_posit + pMainBody2->GetMatrix().m_psoit).Scale (0.5f);
ndJointFix6dof* const joint = new ndJointFix6dof(matrix, pMainBody, pMainBody2)
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Mon Jun 19, 2023 3:01 pm

Ok - so you are defining a mid point between the two positions and placing the joint there.

I adjusted the code as such (didn't do the joint pointer fix yet, I wanna understand this first):

Code: Select all
        origin.m_posit.m_y += 20.f;
        auto pMainBody2 = AddBox(scene, origin, 1500000.0, 4.0, 8.0, 8.0);
        ndMatrix Gmatrix(ndGetIdentityMatrix());
        Gmatrix.m_posit = ((pMainBody->GetMatrix().m_posit + pMainBody2->GetMatrix().m_posit).Scale(0.5f));
        ndJointFix6dof* const joint = new ndJointFix6dof(Gmatrix, pMainBody, pMainBody2);
        ndSharedPtr<ndJointBilateralConstraint> jointPtr(joint);
        ndWorld* myworld = scene->GetWorld();
        myworld->AddJoint(jointPtr);


But there is no change, the two boxes are still on top of each other.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Mon Jun 19, 2023 3:30 pm

the bod has a mass of 1500000.0
what is the other body mass?
if it is around 1, it will only work in double.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Mon Jun 19, 2023 3:33 pm

this is how the test looks like,

Code: Select all
void ndBasicRigidBody (ndDemoEntityManager* const scene)
{
   // build a floor
   BuildFloorBox(scene, ndGetIdentityMatrix());
   
   ndMatrix origin1(ndGetIdentityMatrix());

   //AddSphere(scene, origin1, 1.0f, 0.5f);
   //AddCapsulesStacks(scene, origin1, 10.0f, 0.5f, 0.5f, 1.0f, 1, 2, 7);
   AddCapsulesStacks(scene, origin1, 10.0f, 0.5f, 0.5f, 1.0f, 10, 10, 7);
   //AddCapsulesStacks(scene, origin1, 10.0f, 0.5f, 0.5f, 1.0f, 4, 4, 4);
   //AddCapsulesStacks(scene, origin1, 10.0f, 0.5f, 0.5f, 1.0f, 2, 2, 7);

   ndQuaternion rot;
   ndVector origin(-60.0f, 5.0f, 0.0f, 1.0f);
   scene->SetCameraMatrix(rot, origin);
}


where did you add that test?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Mon Jun 19, 2023 4:29 pm

Aha - I actually have a ndBasicRigidBody.cpp version where that part is disabled (#if 0), and the other code ( ndSharedPtr<ndModel> dragLine(new ndModelDragLine(scene, origin1));) is enabled.

So - I added my code right after the pMainBody is added:

Code: Select all
class ndModelDragLine : public ndModelArticulation
{
    public:
    ndModelDragLine(ndDemoEntityManager* const scene, ndMatrix& origin)
        :ndModelArticulation()
        ,m_controlJoint(nullptr)
    {
        auto pMainBody = AddBox(scene, origin, 1500000.0, 4.0, 8.0, 8.0);

// Misho
        origin.m_posit.m_y += 20.f;
        auto pMainBody2 = AddBox(scene, origin, 1500000.0, 4.0, 8.0, 8.0);
        ndMatrix Gmatrix(ndGetIdentityMatrix());
        Gmatrix.m_posit = ((pMainBody->GetMatrix().m_posit + pMainBody2->GetMatrix().m_posit).Scale(0.5f));
        ndJointFix6dof* const joint = new ndJointFix6dof(Gmatrix, pMainBody, pMainBody2);
        ndSharedPtr<ndJointBilateralConstraint> jointPtr(joint);
        ndWorld* myworld = scene->GetWorld();
        myworld->AddJoint(jointPtr);
//


Also - The weight of the box I added is the same as the main box: 1500000.0

This is what I have (I am dangling the joined boxes using a mouse):

Image
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Mon Jun 19, 2023 4:57 pm

the problem is that AddBox, does a simply object placement by casting a sphere down,
so even when to pass y = 20, the matrix will be set at 12 whis is flush wi the first box.
if you do this

Code: Select all
        // Misho
        origin.m_posit.m_y += 20.f;
        auto pMainBody2 = AddBox(scene, origin, 1500000.0, 4.0, 8.0, 8.0);
        ndMatrix m(pMainBody2->GetMatrix());
        m.m_posit.m_y = 20.0f;
        pMainBody2->SetMatrix(m);


it should work.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Mon Jun 19, 2023 5:23 pm

Aha - yes, that finally worked!! So basically, create an object, THEN update its position...

Thanks!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Mon Jun 19, 2023 6:20 pm

Ok - so the next step I want to test is as follows:

Do you remember a while back when I was asking how to attach an object to another object, whose position and orientation is controlled by an outside input of the position/attitude matrix? Basically - I needed to attach a fuel tank to an aircraft in the Flight Simulator, but that aircraft is being controlled by Flight Simulator's flight model, NOT by Newton. So, the ask is to create a Newton body whose position and attitude is explicitly set. I think at that time you said I need Newton 4.00 to be able to do this.

So basically - I want to try this in the DemoSandBox - I want to hard-attach pMainBody2 to pMainBody (which I have now), and then set up some key press events that will update pMainBody's position in x and z (for now). I would expect to be able to move pMainBody around in X-Z plane, while pMainBody2 stays attached to it.

Any thoughts on how to accomplish this?
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Porting from 3.xx to 4.00 - need help

Postby JoeJ » Tue Jun 20, 2023 4:50 am

misho wrote:Any thoughts on how to accomplish this?


You may want to think about / describe more precisely what 'this' should be.
Here are some options, sorted by increasing amount of simulation.
I assume you start with a target transformation matrix for the spaceship, calculated from what the flight simulator gives you for the next frame.

1. Just teleport the spaceship to the target transformation, calculate the offset for the fuel tank and teleport this too.
That's no simulation at all, and there would be no point to use Newton.

2. Calculate the velocity to get from the current state to the target in one frame, and apply the velocity to the Newton body for the spaceship. Do the same for the fuel tank.
Newton then handles the integration of velocity to get to the target, and it could react to unexpected collisions with an asteroid for example. But because you set velocities instead letting Newton solve for them, the collision response might be still bad.

3. Calculate the force which gives the target velocity as in 2. And do the same for the fuel tank. Apply the forces (and torques) to the Newton bodies.
Now Newton has full control and can solve for realistic collision response or other things.
So that's ideal. But there might be some lag with following the flight simulators target, which shouldn't but might be a problem.

You might want to try multiple options, and it would be good to know how you have handled this before and what worked for you to give specific advise.
I also wonder how you feed back Newton simulation results to the flight simulator.
E.g. we have such asteroid collision, but the flight simulator does not know about the asteroid.
How do you make it react to the unexpected change in spaceship trajectory?

Edit: I have proposed to control both bodies, simply because this reduces the error the fixed joint connecting them has to deal with. You could also control just one body, and make the other follow automatically due to the joint. But that's not really relevant yet for the broader question of controlling position, velocity or acceleration.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Tue Jun 20, 2023 11:46 am

Hi JoeJ!

Thank you for taking time and giving this a thought, I really appreciate it. Sure, I can explain it better, with diagrams and all :D - first - 'this' is not the space simulator part. I have figured that out. I have a robust layer that can spawn objects into Flight Simulator world and let Newton govern their motion. For the space simulator, ALL of the objects are under NEWTON.

Now, for 'this' next part, I want to expand flight simulator world with a physics "upgrade". Flight Simulator lacks any extended physics functionality beyond an aircraft object, and the flight model that is applied to it from the flight simulator. So, let's say, there is a jet fighter. And let's say, a user wants to have a jet fighter with extra fuel tanks on its wings. Right now, for that to happen in a Flight Simulator, a NEW jet fighter 3D model needs to be created WITH extra fuel tanks on its wings, so there will be 2 different versions of the jet fighter 3D model - one with and one without fuel tanks on its wings (and the user can chose the appropriate "version" in the Flight Simulator in-game interface). Flight Simulator currently has no way of attaching these fuel tanks as separate 3D objects.

Now, I HAVE a way of accurately attaching 3D objects to other 3D objects in Flight simulator using Newton, BUT, only to Newton controlled objects (this is how my space simulator works). I have also successfully attached a Newton object to a non-Newton object (aircraft), but it only works when the aircraft is stationary, on the tarmac. I did that to check the math of properly positioning and aligning objects in an Earth-sized cartesian coordinate system with spherical surface. As soon as the aircraft starts to move under FS flight model, the Newton object is "left behind" because I need to update the Newton object's position and attitude, and I don't know how.

So that's the ask: Given a Flight Simulator controlled jet fighter, I need to create a Newton body (green):
Image

Then, I need to align green Newton body position and orientation with the jet fighter's position and orientation:

Image

This green newton body would not have visual mesh or geometry, it would be used as a dummy connector object.

Then, I would create a Fuel Tank Newton object (blue), WITH visual geometry:

Image

... and attach it using Newton to the green Newton object:

Image

Now - the fuel tank will appear to be attached to the wing of the STATIONARY jet fighter. So far, I have done this and it is working properly. Next - when the jet fighter MOVES, I am able read its position and orientation from the Flight Simulator software interface. I need to assign this position and orientation to the green Newton dummy link object, so that it moves WITH the jet fighter:

Image

However, right now, what is happening is, I am unable to set position and orientation to the green Newton object, so that when the jet fighter moves, the Newton objects get left behind:

Image

I tried setting green newton object position and orientation on every cycle, but I am getting a weird lag when the jet fighter is in motion - the fuel tank appears twitching behind the jet fighter. When I stop the motion, the fuel tank "catches up". Julio and myself talked about this a while ago, and he said this WAS possible in Newton 3.xx, but it would be much easier in Newton 4.00.

So - that's the problem. How do I create and set up a Newton object which I can move and orient by directly setting values on its spatial matrix? I my test case, as I was mentioning, I would need to basically create a parent object, attach a dummy object to it, and then assign some keys that update parent objects' spatial matrix. I can do that and see what happens, but my guess is, as Julio was mentioning, some setup needs to be done first with the parent object.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Porting from 3.xx to 4.00 - need help

Postby JoeJ » Tue Jun 20, 2023 12:44 pm

Ok, this makes it very clear.
I did not want to interrupt your discussion with Julio. I just thought, from given context he might need to do too much guessing on what you actually want, increasing the risk to waste time for both of you.

So if he already has something in mind about the lag problem, he can give better advice.
I would have some ideas, but not sure how well they would work, so we rather wait on his response. 8)
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Tue Jun 20, 2023 12:55 pm

Would a compound collision shape do it?

So basically you make the airplane a kinematic body, with a compound shape.

Them you set the velocity and angular velocity
On each simulation step
This will make interact with the world while is be driven by your simulation.

The you add logic to attach and detach parts.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby JoeJ » Tue Jun 20, 2023 1:28 pm

I remember we had proposed to use compounds instead fixed joints before,
but the first problem seems not that, but to make a single body representing the jet following the jet precisely, without lag.

My idea would be to use a kinematic body, setting it's transform it to match the jet each frame.
But to make it interact with other bodies (the fuel tank) correctly, setting transform is not enough.
We also need to set velocity.
Ideally velocity is given for the current frame.
If not, we could approximate it by calculating it from the previous and current jet transform.

But in any case i expect some small error, which might be a problem for the joint or not.
I also never used a joint between a kinematic and a rigid body, and idk if this works at all. I guess it does work, though.
If not, using a rigid body for the jet body should work too, but since they are not meant to be teleported each frame, i'd try the kinematic first.

I'm also unsure about the mass properties for the jet body. Likely it should match the jet, but eventually it's better to set a higher mass so the tank does not drag it too much.

Regarding fixed joint vs. compound, the joint approach has an advantage i have not thought about back then: Using a compound, the inertia of it will not precisely match the intended sum of the two separated bodies. So if we release the compound to detach the tank, i expect some minor discontinuity.
Thus, if the joint approach works robustly, it's indeed the better solution even if more expensive and less stable.
If it's not perfectly robust but jitters, affecting the tanks velocity to match the jet either directly or with external force (as initially proposed) might help.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Porting from 3.xx to 4.00 - need help

Postby Julio Jerez » Tue Jun 20, 2023 1:41 pm

JoeJ wrote:I remember we had proposed to use compounds instead fixed joints before,
but the first problem seems not that, but to make a single body representing the jet following the jet precisely, without lag.


I do not think he had a choice. the airplane is given by a simulation other than the physics engine.
the only thing he can do is to query the state: position, orientation, velocity and angular velocity.

you are correct if you only use velocity and angular velocity, after a while there will be a lag
but he can use the position and orientation only to extract the velocity needed to match the position and orientation, that is

v = (p1 - p0) / dt
w = (q1 - q0) / dt

here p0 and q0 are the kinematic body position and p1, q1 are the position and rotation of the airplane given by the simulation.
then he feeds that to the engine in every step, and the lag is limited to one step.
it actually works quite well.

there are function in the math library to extract omega from rotation.

ndVector CalcAverageOmega(const ndQuaternion &q1, ndFloat32 invdt) const;

on the mass, that's the beauty of the kinematic body, the body move kinematically, meaning teh mass is not part of it. it is as if is playing and animation.

the mass is considered for the integration with rest world. so given a reality mass help on how the rest of the world would react.

and yes, if say a gas tank is detached for some action in the game,
it needs some logic to make that part a dynamics body after detachment, but that's part of the game logic.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Porting from 3.xx to 4.00 - need help

Postby misho » Tue Jun 20, 2023 2:14 pm

Hi Julio, JoeJ!

Yes - I don't have the choice, the Flight Simulator is providing physics for the aircraft, but ONLY that. It does that because it is a Flight Simulator and it does that well, but only that :D. If I wrote a BETTER flight model in Newton, then yes, the problem would be solved, because then the aircraft would be controlled BY Newton. But I don't want to do that because Flight Modelling is a science in itself, and MS's flight model is very good as it is. And yes, I have access to airplane's position and orientation, and perhaps velocities (Except for velocities but they seem to be using a weird coordinate frame which I haven't been able to figure out yet). In any case, as Julio mentioned, I can compute velocities and omegas from previous/current position/orientation.

And yes, I would like to be able to obviously detach the fuel tank, and eventually mount weapons (rockets) as well, since I have the rocket propulsion going already. Also, the ideas is to create a hollow "hold" inside a transport aircraft and be able to drop cargo in flight, stuff like that.

JoeJ, yes I remember you have proposed using compounds, but the fixed joints worked rather well, even at high orbital speeds. I have a whole space station assembled in orbit, and it holds firm and steady. This is in Newton 3.14.

It really does come down to just one problem: To directly set Newton body's position/orientation, or velocities derived from position/orientation.

So, something just dawned on me - a question:
In the DemoSandBox, how is mouse interaction with objects done? What happens when you "grab" an object and move it around? Because that is exactly what I need, an external manipulation of Newton object!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests