Unity plugin progress?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 5:09 pm

Amazing, I did not see that one coming?
you are killing me harry? :shock: :shock:
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 4:28 am

Saw that you tried the GetOwner function.
But we need to call SetOwner first.
Adding this to InitRigidBody should do the trick.

Code: Select all
m_body.SetOwner(this);


But i tried it and GetOwner returns null when OnCollision is returned.
I couldn't understand why at first but then I realized m_world.GetBody0 and m_world.GetBody1 is the problem.

This code(generated by swig) returns a new c# dNewtonBody instance and m_owner will be null.
Code: Select all
  public dNewtonBody GetBody0(global::System.IntPtr contact) {
    global::System.IntPtr cPtr = NewtonWrapperPINVOKE.dNewtonWorld_GetBody0(swigCPtr, contact);
    dNewtonBody ret = (cPtr == global::System.IntPtr.Zero) ? null : new dNewtonBody(cPtr, false);
    return ret;
  }


Maybe we could store the adress of NewtonBody(c#) in the native body instead.

You can use this code to get an intptr from a c# object.
Code: Select all
var handle = GCHandle.Alloc(myNewtonBody);
IntPtr myPtr = GCHandle.ToIntPtr(handle);

This IntPtr can be passed at creation to the native NewtonBody.
Now the native body keeps a reference to the managed object.

To get the managed object from the intptr you use this...
Code: Select all
var handle = GCHandle.FromIntPtr(intptr);
var myBody = (NewtonBody)handle.Target;


But referencing the managed object like this also prevent the Garbage Collector from touching the managed object so we must free the handle when we are done with it.
Like this...

Code: Select all
DestroyRigidBody()
{
var handle = GCHandle.FromIntPtr(intptr);
handle.Free();
}


So, GCHandle lets native code keep track of managed objects.
GCHandle can also pin the adress of a managed object as well but we don't need that, thats only if you have a managed arrays or buffer that you want native code to write to.
The reason you can't just get the adress of a managed object is that the GC may move the object around in memory for optimization. The GCHandle helps us keeping track of the managed object even if it moves. Pinning the object prevents it from moving as well but as I said we don't need that, we only want to keep track of it.

[EDIT]

Hmm, just realized NewtonBodySetUserData is already used for keeping a reference to dNewtonBody... :?
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 21, 2017 10:05 am

can you made those changes and commit them?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 10:27 am

I'll give it a try :)
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 11:08 am

Ok, i've checked in my changes.

So far it seems to work.
I had to uncomment your changes in the GetNextContact and GetFirstContact because thoses changes caused Unity to hang.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 21, 2017 11:51 am

good, It was hanging on my changes, but is fixed now.

I am tryin to add trigger functionality. I am thinking to change how triggers works.
In previous version newt do no have triggers, it was all implemented as part of the custom joint system

however this is double work, because a trigger essentially is a list of how many object are in a region s space define by a collision shape.
But that's what the contact joint is, is I add a field indicating that when the contact joint first collide with a shape, is in the state and when is no longer on contact. then we can query that contact joint for that state.
This does not requires build a system to keep track of trigger volumes managers. or a lest made the implementation much simpler.
I see if I can make that change tonight.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 4:30 pm

We didn't have a demo_03 so I added a scene that demonstrates OnCollision.
A couple of boxes that fly up in the air as soon as they touch the ground.(I'm not very creative) :lol:

Added a button to spawn new bodies. Now we know that works.
Had to call InitRigidBody manually though.
We should probably call InitRigidBody in the Start event of NewtonBody as well. Should work if we check if m_body is null first.
That way it won't matter if both NewtonWorld and the GameObject calls InitRigidBody.

[EDIT]
Ah, forgot. The GameObject can't call InitRigidBody because the world may not be created yet.
We have to call InitRigidBody manually for now.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 21, 2017 5:27 pm

Oh cool, now we can spawn bodies at run time, nice.

I am trying to make the changes to support trigger volumes. I do not want to make it the same way was done in 3.13, I'd like a lower level support since it seems a very useful functionality for game mechanic.

Triggers are just like contacts but they do not generate contacts point, two bodies can state in overlapped state indefinitely, and the higher lever get notification.
we can do that with contacts by it requires lots of work that should not be necessary.

when I have ready we can probably modify that demo so that when a body is inside a trigger volume
it does something like changing color or something like that.
we can make the trigger a transparent volume, so that is clear to the end user.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 21, 2017 5:30 pm

Ah, forgot. The GameObject can't call InitRigidBody because the world may not be created yet.
We have to call InitRigidBody manually for now.


I do not really see calling initRigid body as a bad thing.
I agree that before having to figure out a scene index was bad, but now that is not a requirement
we is that so onerous?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 5:35 pm

A boyancy demo would be nice. I suppose triggers would be useful to simulate bodies of water.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 21, 2017 5:37 pm

Julio Jerez wrote:
Ah, forgot. The GameObject can't call InitRigidBody because the world may not be created yet.
We have to call InitRigidBody manually for now.


I do not really see calling initRigid body as a bad thing.
I agree that before having to figure out a scene index was bad, but now that is not a requirement
we is that so onerous?


Well, Unity hangs if you forget to call it. We could add a console warning if the user forgets to init the body.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 21, 2017 6:05 pm

oh I see it hangs because it has a body but is not registered with the world.

This is actually a bigger problem, I watched some unity bodies this weekend I notice that disabling game objects is a big part of Unity game mechanic.
we need to fin a way to emulation that functionality.

I suppose the goal would be if a body is not initialized then it is simple ignored.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Wed Feb 22, 2017 2:48 am

we got triggers now, I just put a simple test using the friction demo, later we can make a better one.
The buoyancy sound like a goo test case yes.
Basically I had a bug in the engine that I had to fix and now is working.

There is nothing special about triggers really.
In newton a trigger is just a convex collision shape that can generates contact joints but not contact points.
anyway, now I see if I can finish the first game demo, and the start the Ray and convex cast interface.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Wed Feb 22, 2017 5:45 am

Added a simple trigger demo where the trigger acts like a body of water.

The script is currently assigned to the moving body which detects when it is inside the trigger and apply some simple "boyancy" force.

Ideally I would instead like to have a script on the trigger that add forces to any body that collides with it, that is, I would rather have the trigger volume add the forces. But I don't know how to make the script on the trigger object to apply forces on other bodies.

Could we add some kind of "External force accumulator" to NewtonBody that can be called from outside the OnTorqueAndForce callback?

[EDIT]

Aha, noticed you already added force accumulators to NewtonBody.
We just weren't applying their forces on update.
I did that and now the script is running on the water trigger instead and apply forces to all bodies that enters. :D
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Wed Feb 22, 2017 8:58 am

nice!!,
since we have that demo now I will quickly add the utility function that calculate, a more realistic Archimedean buoyancy force
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 17 guests