Sounds good.
Let's start very easy this time with just the objects needed to create a world, a simple collider and the body.
Then we can verify that swig wraps the parameters right so that C# can pass and retrieve data in a effective way.
Moderator: Alain
// If exported by a plugin, this function will be called when the plugin is loaded.
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
// If exported by a plugin, this function will be called when the plugin is about to be unloaded.
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
Sweenie wrote:Since NewtonSDK inherits from MemoryAlloc the two methods __MemoryAlloc_Alloc__ and __MemoryAlloc_Free__ are exposed.
Is that intended?To be honest I'm not really sure what they will be used for in C#.
[AddComponentMenu("Newton Physics/Colliders/Box Collider")]
public class NewtonBoxCollider : NewtonCollider
{
//public members will be editable in the Unity Editor
public Vector3 Size = Vector3.one;
public override IntPtr CreateCollider(bool applyOffset)
{
Matrix4x4 offsetMatrix = Matrix4x4.identity;
if (applyOffset)
offsetMatrix.SetTRS(transform.localPosition, transform.localRotation, Vector3.one);
IntPtr collider = NewtonAPI.NewtonCreateBox(NewtonWorld.Instance.pWorld, Size.x, Size.y, Size.z, 0, ref offsetMatrix);
NewtonAPI.NewtonCollisionSetScale(collider, Scale.x, Scale.y, Scale.z);
return collider;
}
}
Colliders can be added to an object without a Rigidbody component to create floors, walls and other motionless elements of a scene. These are referred to as static colliders.
In general, you should not reposition static colliders by changing the Transform position since this will impact heavily on the performance of the physics engine.
Colliders on an object that does have a Rigidbody are known as dynamic colliders.
Static colliders can interact with dynamic colliders but since they don’t have a Rigidbody, they will not move in response to collisions.
#include "IUnityInterface.h"
extern NewtonSDK* sdk;
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
sdk = new NewtonSDK();
}
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
delete sdk;
}
Sweenie wrote:There is one other way though.
If the C++ NewtonWrapper implement these two functions...
- Code: Select all
// If exported by a plugin, this function will be called when the plugin is loaded.
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
// If exported by a plugin, this function will be called when the plugin is about to be unloaded.
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
we get two callbacks from Unity.
That way we can have an instance of NewtonSDK ready when the plugin loads and it destroys itself when the plugin unloads...
[DisallowMultipleComponent]
[AddComponentMenu("Newton Physics/Newton World")]
public class NewtonWorld : MonoBehaviour
{
//Exposed to Editor
public int SolverMode = 1;
private NewtonSDK m_sdk = null;
internal NewtonSDK GetNewtonSDK()
{
if (m_sdk == null)
{
m_sdk = new NewtonSDK(); // Creates a new NewtonSDK object an keeps a reference to it.
m_sdk.SetSolverMode(SolverMode);
}
return m_sdk;
}
void FixedUpdate()
{
if (m_sdk != null)
m_sdk.Update(Time.deltaTime);
}
void OnDestroy()
{
m_sdk = null; // Release reference to the NewtonSDK object;
// Don't call m_sdk.Dispose() , bodies may still hold a reference to it.
// m_sdk will be collected and disposed by the Garbage Collector when all Bodies released their reference to it.
}
}
[DisallowMultipleComponent]
[AddComponentMenu("Newton Physics/Newton Body")]
public class NewtonBody : MonoBehaviour
{
//Exposed to Editor
// User connects to a GameObject with a NewtonWorld in the Editor
public NewtonWorld World = null;
public float Mass = 1.0f;
private NewtonSDK m_sdk = null;
private BodySDK m_body = null;
void Start()
{
if (World == null)
{
Debug.LogError("You must connect this body to a world.");
return;
}
// If this is the first call to World.GetNewtonSDK, the NewtonWorld will create a NewtonSDK object, if not NewtonWorld will just return the already created NewtonSDK object
m_sdk = World.GetNewtonSDK();
m_body = m_sdk.CreateBody();
}
void OnDestroy()
{
m_body = null;
m_body.Dispose(); // Dispose will immediately release the unmanaged resource.
m_sdk = null; // Release reference to NewtonSDK object. If this was the last reference to the NewtonSDK object it will be disposed by the Garbage Collector.
}
}
using UnityEngine;
using System;
namespace NewtonPlugin
{
[AddComponentMenu("Newton Physics/Joints/Hinge Joint")]
public class NewtonHinge : MonoBehaviour
{
IntPtr joint = IntPtr.Zero;
private NewtonBody childBody = null;
public NewtonBody ConnectedBody = null;
private bool JointCreated = false;
public float Friction;
public bool EnableLimits;
public float MinAngle;
public float MaxAngle;
void Awake()
{
if(CreateJoint())
JointCreated = true;
}
void Start()
{
// for the case that joint creation didnt happen in the awake()
if(!JointCreated ){
while( !(childBody = GetComponentInParent<NewtonBody>()) )
{
// you can make a coroutine in the start function so wee wait half a second and try again
yield WaitForSeconds( 0.5 );
}
CreateJoint();
}
}
}
void Update(){
}
void OnDestroy()
{
Debug.Log("Destroying hinge");
//NewtonPlugin.DestroyJoint(joint);
}
bool CreateJoint(){
Debug.Log("Creating hinge");
childBody = GetComponentInParent<NewtonBody>();
if(childBody == null)
return false;
Matrix4x4 matrix = Matrix4x4.identity;
matrix.SetTRS(transform.position, transform.rotation, Vector3.one);
IntPtr parentBodyPtr = IntPtr.Zero;
if (ConnectedBody == null)
return false;
parentBodyPtr = ConnectedBody.pBody;
joint = NewtonAPI.NewtonCreateHinge(ref matrix, childBody.pBody, parentBodyPtr);
NewtonAPI.NewtonHingeEnableLimits(joint, EnableLimits);
NewtonAPI.NewtonHingeSetLimits(joint, MinAngle * Mathf.Deg2Rad, MaxAngle * Mathf.Deg2Rad);
NewtonAPI.NewtonHingeSetFriction(joint, Friction);
}
}
}
Sweenie wrote:Based on Julios suggestion I think we will do something like this.
Explanation comes later, I have to go buy some food now.
public class NewtonBody : MonoBehaviour
{
//Exposed to Editor
// User connects to a GameObject with a NewtonWorld in the Editor
public NewtonWorld World = null;
public float Mass = 1.0f;
private NewtonSDK m_sdk = null;
private BodySDK m_body = null;
NewtonBody.Cons: The user world have to add the world(or worlds) to the scene and assign it to every
Users browsing this forum: No registered users and 2 guests