Unity plugin progress?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Unity plugin progress?

Postby Sweenie » Thu Feb 16, 2017 9:16 am

actuators made life easy, don't they?


They were easier to use then i thought. Just set the target position/angle and speed and away it goes. :D

Creating different four-bar linkage constructions were alot more difficult though. :?
Easier ones like the crank, and the windscreen wiper i added was easy but it quickly gets complicated if you want something more advanced.
Sweenie
 
Posts: 354
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 10:13 am

check out the box car I just committed.
it has a motor and a differential.
see if you understand it.

I had some issues with scale, in unity there is not way to freeze scale on primitive or at least I do not know how therefore children objects inherited parent scale and that causes problem when you make a tire which uses non uniform scale and it is a child of a box witch also has a non uniform scale.

anyway the motor is a universal joint that control two axis of rotation, one for moving forward and backward, and the other for differential steering.
They both move at constant speed so this will be the equivaled of an electric motor.
It is also possible to apply a torque to the motor in one axis leaving the other free, and apply steer to the front tires, this will be a normal vehicle.
For slow moving vehicles, we can make all kind of configuration now.

For high performance cars this will not work because we need a proper tire friction model.
we will add the vehicle joints later which do implement a Tire brush contact model and that will make realistic high speed vehicle that can drift and power slide.

At this point I believe we have enough joints, we can always add more on demand.
No I will add the ray cast and convex casting. which is important to have a more complete frawork.

Do you think you can make a player script to drive the little car around and add some interesting challenges?
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 10:20 am

I parented it from a dummy game object so that is easy to move around

Sweenie wrote:.. but it quickly gets complicated if you want something more advanced.

oh yes, but I want to go to the advance, if you know what I mean. :wink: :wink:
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 10:26 am

Oh darn, you added more stuff to the robot :D :D
now fancy crank, and color!!
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Thu Feb 16, 2017 10:42 am

Yeah, it quickly gets messy when you start messing with scales.

I general it's usually best to avoid scaling but in Unity there isn't a way to define sizes for primitives other than using scaling.

Scales are evil, it messes with your head. :twisted:

Another approach is to do as I mentioned earlier when we discussed ragdolls. Group and separate components. For example Model is in one GameObject and the Rigidbody and collider in one.

For example a car.

Start by creating a dummy game object as a root node.
Then under that create a GO called chassiBody(Newtonbody + collider), another GO called chassiModel(Mesh).
Do the same for the wheels.

So in the end it looks something like this...

Code: Select all
Car
-->ChassiBody(NewtonBody + Collider)
-->ChassiModel(Meshrender + mesh)
-->FLWheelBody(NewtonBody + Collider)
-->FLWheelModel(Meshrender + mesh)
-->FRWheelBody(NewtonBody + Collider)
-->FRWheelModel(Meshrender + mesh)
-->RLWheelBody(NewtonBody + Collider)
-->RLWheelModel(Meshrender + mesh)
-->RRWheelBody(NewtonBody + Collider)
-->RRWheelModel(Meshrender + mesh)


That way you can scale the model any way you want without messing with the wheels.

A simpler setup could be like this...

Code: Select all
Car
-->Chassi(NewtonBody + Collider + Mesh)
-->FLWheel(NewtonBody + Collider + Mesh)
-->FRWheel(NewtonBody + Collider + Mesh)
-->RLWheel(NewtonBody + Collider + Mesh)
-->RRWheel(NewtonBody + Collider + Mesh)


[EDIT]
Ah, just saw you did that. Makes it a bit easer to work with. :)
Sweenie
 
Posts: 354
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 5:45 pm

Ok I said the next step was adding the raycast interface, but before that we need to do the materials.
here I am not sure how to go.

in one side the easier way is doing what every one else does and make material a property of the shape and when two shape collide use a rule to determine what property to chose.
This is easy but I considered and a abomination.
the other method is using a graph that relate two material and the edge specify the interaction.

this sound like a combinatorial however is my experience that the graph always end up being sparse.
take for example a terrain, it can have many materials: mug, pavement, concreate, dirt and so on
but these material never interact with each others.

I see that unity has some special object the call asset, which ideal for this kind of interface.
basically we can create these interactions as asset, and the take two ids:
say:
material_1 = CONCRETE
material_2 = METAL
...
...

then when we create the shape we assign one of these two ID form a drop down material ID.

to make it easy to set simple scenes we make the default material, which can be configure in the world. we can set a rule that is an inertartind does not exist, the call back default to the default material, so it is up to the user to add the integrations as they see fit.

we can also add a notification call back the we can specify he cases where the user want to deal with contacts. for example say we have a default - default function, that we want to manipulate the friction coefficient. by adding the notification, then when that shape participate in a collision it will get a callback to the world with the tow shapes, and here it can override the parameter as it see fit.

what do you think?
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 6:15 pm

I am trying to add a player driver script to the box car, but I get a hard crash immediately, not sure what is wrong, is vey much the way you added the script to the robot.

I can not see why it crasher, but the moment I add this line is does.
motor = transform.Find("motor").GetComponent<NewtonBody>();
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 6:44 pm

here is another problem that I realize we may have.

Unit has two event function that is call by unity: Update and FixUpdate.
we know how those works,
it seem we may use that to control objects but that will be a mistake because the operation will be out of synch with the physic update.
The newton update has it own update function that call force an torque for rigid bodies.
but the rigid body is part of the plug in,
I what we nee to do is, I think, iterate over each Gameobject and check for scripts, and for each script in the script has and NewtonUpdate method, then call that function. But I do no know if this is possible.
it is possible to query a component to see if it implement a method?
I suppose we can make a base class script like this

Code: Select all
abstract public class NewtonScript : MonoBehaviour
{
    abstract public void OnNewtonUpdate(NewtonWorld world);


...

}


then script are derived from the base class an implement interface,
that was we can iterate over those script
is there an more elegant or better way of doing this?
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 7:46 pm

Ok I found out the crash problem, I attached two script to two different objects, no sure why that made crash by after I remove one, now it works.
I see if I can make drive the car, the see if we can make the base class script so that newton control happens on NewtonUpdate. if this work then we can simply the newton Body interface as well, I do not like the actions tuff.
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Thu Feb 16, 2017 9:13 pm

ok I added a very basic script that allowed for controlling the car.

Is not very good, I think the default friction is too low, I have to find out how to make it more responding. I has similar problems when making the robot in the SDK.
It also needs a texture to the tires and the engine so that we can see who is going on.

The cool think is that we can export that model and play it in the demo sand box and debug it there.
for now is good enough, I think.
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Fri Feb 17, 2017 3:16 am

Julio Jerez wrote:here is another problem that I realize we may have.

Unit has two event function that is call by unity: Update and FixUpdate.
we know how those works,
it seem we may use that to control objects but that will be a mistake because the operation will be out of synch with the physic update.
The newton update has it own update function that call force an torque for rigid bodies.
but the rigid body is part of the plug in,
I what we nee to do is, I think, iterate over each Gameobject and check for scripts, and for each script in the script has and NewtonUpdate method, then call that function. But I do no know if this is possible.
it is possible to query a component to see if it implement a method?
I suppose we can make a base class script like this

Code: Select all
abstract public class NewtonScript : MonoBehaviour
{
    abstract public void OnNewtonUpdate(NewtonWorld world);


...

}


then script are derived from the base class an implement interface,
that was we can iterate over those script
is there an more elegant or better way of doing this?


The NewtonScript example above is the the first thing that came to my mind.
Then on start Newtonworld should be able to find them and keep them in an array.
FindObjectsOfType
https://docs.unity3d.com/ScriptReferenc ... fType.html
Hopefully that function works the way I think.

Though we could always use reflection.
It should work equally well to loop through the scene and do this check on every component.
Code: Select all
if(component is NewtonScript)

The "is" operator checks if an object is of that type or is derived from it.

I think we can check specifically for a method as well.
Using this code
Code: Select all
public static bool HasMethod(this object objectToCheck, string methodName)
{
    var type = objectToCheck.GetType();
    return type.GetMethod(methodName) != null;
}


Unity also the the function SendMessage and BroadCastMessage but that would be too slow for this.

Oh, forgot about interfaces. This could be an option as well...

Code: Select all
We add this to the Plugin
interface INewton
{
    void OnNewtonUpdate(NewtonWorld world);
}

Then you could declare you script component like this
public class MyRobotController : MonoBehaviour, INewton
{

}


And again use "is".
Code: Select all
if(object is INewton)


But I think we should start with making the NewtonScript class that extends the MonoBehaviour.
Other than OnNewtonUpdate later we probably would want to add more functions, such as OnCollision, OnTrigger, OnSubmitConstraint(!?).
The user then just add the functions he wants.

The interface variant above would force the user to implement every function in the interface and that would be bad design i guess.
Sweenie
 
Posts: 354
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 12:40 pm

I start to add the physics materials, but I am stocked, because I do no know what king of script object are assets.

in the Unity editor there is one tab name assets.
under the create submenu is has Physics material, and physics material 2d

we could use that, but this is not exactly what I envision.
I envision a material as an objet that hold two elements, say metal ID and Concrete.
is also hold the propertied between those to material and so on.

the problem is that I do no see what king of objects are those asset, they do not seem to be c# code.
by the most be because when they are use in a collider, the collider has a reference to them.
has you played with that?

in the documentation they talk about assets bundle, but I see no example or I do no even know it Is the same thing.
https://docs.unity3d.com/ScriptReferenc ... quest.html

I also found this but is confusing
https://forum.unity3d.com/threads/creat ... rs.388074/
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Sat Feb 18, 2017 12:59 pm

Just create the class the same way we create newtonbody, newtoncollider and so on.

You want the material to be a component that you add to the gameobject, right?
Sweenie
 
Posts: 354
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 1:27 pm

No the material is no added to the game object.
The collision will have a field to get an material id,
Material should be some kind of asset that is no really a game objet nor a component,

as a test I wrote this

Code: Select all
[CreateAssetMenu(menuName = "Newton Material")]
public class Newtonxxxxxxxx : ScriptableObject
{

}


and now register and item in the assets/create menu.
maybe now we can populate it with moment and see if t work.

basically the next step is that the world will have the material graph that will find all the different IDs and stablish the connections.
each two IDs will map to one of those assets Is the asset exist was create by in the scene, else it will map to the default asset.
by default the world will make the default-default connection.

no sure if this will be possible but this is the idea.

I committed the first example, if doe no do anything yet, but see if you get the idea.
Julio Jerez
Moderator
Moderator
 
Posts: 9478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 2:20 pm

ok I no added:
newton Material
newton Material interaction.

this makes a very simple graph that link concrete / concrete.
is no linked or the collider yet, bu is just a matter of change this field for and ID to a Newton Material

the next step I shaving the work scan the scene for all newton Material interaction. and connect that to the newton material callback so that we can read the parameters form.

It should also worm the other way, when make our own NetwonScript base class, there we can get collision event if there are registered, so that any game object can get them (I have no ide how to do that but we will see)

notice that even it can be tedious to build the material graph, the Is by far better that the way Physic material are created, the graph is also static data so it is efficient,

take for example the case of play a sound, the way physic set material each game object get one
event, the way we get one even for both objects.
Julio Jerez
Moderator
Moderator
 
Posts: 9478
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 3 guests

cron