Jump to content

Collision Events


Rekindled Phoenix
 Share

Recommended Posts

The delegates are included, so what is the best way to capture the collision events using the new Headers?

 

Without the Headers using TylerH's old wrapper, I'd call SetEntityCallback like this:

SetEntityCallback(box1, New EntityCallback(AddressOf DoSomething), ENTITYCALLBACK_COLLISION)

 

With the new Headers, do I create something like this? If so, how do I tell Leadwerks to invoke the delegate without the SetEntityCallback function?

 

//C# (translated, may be incorrect)
EntityCollisionCallback del = new EntityCollisionCallback(CollisionHappened);
public void CollisionHappened(System.IntPtr _entity1, System.IntPtr _entity2, Leadwerks.Vector3 _position, Leadwerks.Vector3 _normal, float force)
{
    DrawText(_entity1, 300, 300);
}

EDIT: VB removed

I can't find the 'SetEntityCallback' function in the Core namespace. Where did it go?

Link to comment
Share on other sites

I could as well. I simply lost some interest, but mostly a lot of time to give to Leadwerks development. Klepto has proposed to do it, I will send him the source tonight. Then hopefully, he will send me his modifications and I'll be able to publish V.1.21.

Link to comment
Share on other sites

That would be most helpful. Thank you Ubu.

 

I looked in the old wrapper and found this as the only callback / event handler. Would this help?

 [DllImport(LEADWERKS_DLL, EntryPoint = "SetEntityCallback", CallingConvention = CallingConvention.StdCall)]
       public static extern void SetEntityCallback(IntPtr _entity, EntityCallback callback, int id);

 

A separate boolean with a synclock surrounding it tells my render loop to display "Collisions Yay!". I get random crashes, after a series of them are displayed though, mostly when I'm not currently colliding with anything...

 

EDIT: Added my code (VB)

Dim KeepCallback As New EntityCallback(AddressOf test)

SetEntityCallback(Player.Pointer, KeepCallback, EntityCallbackType.Collision)
'//System.GC.KeepAlive(KeepCallback)

 

After the first collision there's a timespan of a couple seconds with or without collisions, then the app crashes.

*Investigating whyyyyyyy*

 

Oh, and don't forget the function "FreeEntityCallback" !!

Link to comment
Share on other sites

Hi, I will start today to implement the missing functions (As long as i find everything) If someone is functions/delegates he/she is missing in the current release please post them here. So I'm able to better find the missing functions.

 

Also I will add a Leadwerks Control and send it to Ubu, maybe he will add it to the main assemblies.

 

@Rekindeled Phoenix:

This needs some investigation, I remember that it works without issues in the old wrapper maybe something with the delegate definition is wrong.

  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

@Klepto

I used the same code from the original wrapper classes.

Do you see anything wrong with my DLL declared function posted above?

(Will post an example later for debugging)

 

When calling the DLL's SetEntityCallback, the function only works when you set the type as a regular EntityCallback delegate. There are at least 6 other callbacks in the wrapper that can't be used! Why??

 

Can anyone else get callbacks to work without crashing their app?

Link to comment
Share on other sites

No, but I need to to investigate a bit further. If i remember correctly there was a need to Marshal the structs. Yesterday I wasn't able to solve it but hopefully i will solve everything over eastern weekend.

  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

...you can surround the UpdateWorld command with a Try Catch Block. Not elegant but it seems to work.

 

Sounds like a good temporary alternative. Let me know when you've found a solution, it still crashes my game even with error handling. I'm still trying to figure what's causing the problem.

 

EDIT: I can't get the method for FreeEntityCallback to work at all. Try catches seem to be useless as the Engine throws a System.ExecutionEngineException, which isn't caught for some reason.

Link to comment
Share on other sites

So I've tried freeing the entity that checks the callback from LE and Windows memory and found that it delays the crash by about 10-30 seconds. Without removing the object, the application will crash after about 5 seconds, no matter what Garbage Collector settings are assigned to the object.

 

It seems to crash out of the bounds of the App Domain. Whether the OpenGL Window Handle (Leadwerks) is crashing, or whether it's .Net Memory Management causing the issue, I still can't figure it out.

 

Anyone else get collision working yet?

Link to comment
Share on other sites

  • 2 months later...

Even with implementing 'FreeEntityCallback', the LE unmanaged code frees and entity with a managed delegate, and my application crashes. How do I 'detach' the managed delegate from the object before the application frees the memory space??

 

With access to only the delegate / callback in managed code, how do I tell LE to remove the handler for that object so it doesn't crash my program?

 

Could someone please update the headers, and if possible, provide a working code example?

Link to comment
Share on other sites

If you grab the SVN and look at GameEntity.cs, it contains all of the Garbage Collection calls you need to prevent these crashes and safely free everything before being freed :)

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Thanks TylerH, I've found the GameEntity class and converted it to VB, and still am having issues. When the physics body 'AmmoBody' collides with the terrain, OnCollision() within the GameEntity object are fired and so is my DeclareCollisionToScreen() method. Though, the main thread never seems to return to the render loop, throwing an access violation exception. Here is my code:

 

Is this the correct way of implementing collision detection using your GameEntity class?

TylerH, could you provide a working example?

 

VB

'Create a physics body
Dim AmmoBody As Body = Body.CreateBox(0.2, 0.2, 0.2)

'Set it as a GameEntity class object
Dim GameEntityObject As New GameEntity(AmmoBody.Pointer)

'Create handler to throw event
Dim CollisionDelegate As New GameEntity.CollisionEventHandler(AddressOf DeclareCollisionToScreen)
AddHandler GameEntityObject.Collision, CollisionDelegate

'Method that's fired
Sub DeclareCollisionToScreen(ByVal Sender As Object, ByVal e As CollisionEventArgs)
   DrawText(e.Entity0.ToString, 50, 50)
End Sub

 

C# (Online converter)

//Create a physics body
Body AmmoBody = Body.CreateBox(0.2, 0.2, 0.2);

//Set it as a GameEntity class object
GameEntity GameEntityObject = new GameEntity(AmmoBody.Pointer);

//Create handler to throw event
GameEntity.CollisionEventHandler CollisionDelegate = new GameEntity.CollisionEventHandler(DeclareCollisionToScreen);
GameEntityObject.Collision += CollisionDelegate; //is converter correct here?

//Method that's fired
public void DeclareCollisionToScreen(object Sender, CollisionEventArgs e)
{
   DrawText(e.Entity0.ToString, 50, 50);
}

Link to comment
Share on other sites

Found a problem and may be a solution: every call to the GameEntity events must check if the event has been assigned before calling it and must be commented the throw exception in the destructor.

 

namespace Leadwerks.Game
{
   public class GameEntity : Leadwerks.Entity, IDisposable
   {
       ......
       ~GameEntity()
       {
           //throw new System.NotImplementedException();
       }
       ......
       protected virtual void OnFreeEntity(IntPtr entity)
       {
           if (FreeEntity != null)
               FreeEntity(this, entity);
       }
   }
}

 

With those changes to GameEntity I have no exception running the following VB code:

 

   Sub Main()
       Graphics.Initialize(800, 600)
       Framework.Initialize(False)
       Debug.Physics = True
       Collisions.Set(1, 1, True)

       Dim cube As Body = Body.CreateBox(1, 1, 1)
       cube.Mass = 1
       cube.CollisionType = 1

       Dim plane As Body = Body.CreateBox(10, 0.2, 10)
       plane.CollisionType = 1
       plane.Position = cube.Position
       plane.Move(New Vector3(0, -1, 0))

       Dim AmmoBody As Body = Body.CreateBox(1, 1, 1)
       AmmoBody.CollisionType = 1
       AmmoBody.Mass = 1
       AmmoBody.Position = cube.Position
       AmmoBody.Move(New Vector3(1.5, 0, 0))
       Dim GameEntityObject As New GameEntity(AmmoBody.Pointer)
       AddHandler GameEntityObject.Collision, New CollisionEventHandler(AddressOf DeclareCollisionToScreen)

       While (Not Keyboard.KeyHit(Key.Escape) And Not Window.HasRequestedClose())
           Framework.Update()
           Framework.Render()
           Graphics.Flip()
       End While
       Framework.Terminate()
   End Sub

   Sub DeclareCollisionToScreen(ByVal Sender As Object, ByVal e As CollisionEventArgs)
       Drawing.Text(e.Entity0.ToString, 50, 50)
   End Sub

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

I never implemented the destructor?! That would explain it :)

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

That solves part of the problem. Thanks ZioRed

 

Have you tried moving the object over 100 units away from the camera or waited 30 seconds after the collision occurs? The collision events work fine in my code, but now the object is culled or reallocated and ends up killing the process.

 

Why would distance cause a problem??

 

 

Soon I'll compile a working demo, upgrade to VS2010, and update the C# Wrapper to 1.32.8

Link to comment
Share on other sites

Have you tried moving the object over 100 units away from the camera or waited 30 seconds after the collision occurs? The collision events work fine in my code, but now the object is culled or reallocated and ends up killing the process.

...

Soon I'll compile a working demo, upgrade to VS2010, and update the C# Wrapper to 1.32.8

Yes please, post a sample code (with VS2010 and the latest wrapper) to test, so I can test both in NET and in C.

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

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.

 Share

×
×
  • Create New...