I'm using Farseer for physics in a game, and have several key classes: Planet and PlanetProp. Each planet has a large circular body and a "field". This field is a large sensor around the body. PlanetProps are pulled towards whichever planet's field they are currently in (a struct called PlanetInfo). So I've set up a collision handler in PlanetProp which will (whenever it is called) detect whether the fixture it has collided with is a sensor.

I then plan to A) detect if the sensor belongs to a Planet and B) Set the PlanetProp to be pulled towards the planet

So in other words, is it possible to access a class using a collision callback with a fixture from a body in that class?

EDIT: Does this involve UserData? If I was using C++ I might be able to put a pointer to the class in that, but I'm using c#.


1 Answer 1


In C# there you have "reference types" and "value types". Reference types are created with the class keyword. Value types are created with the struct keyword. (Extended explanation on MSDN.)

A reference (for example a "Planet myPlanet" variable if you have class Planet) is very similar to a pointer. It takes the same space (32 bits or 64 bits, depending on your architecture) and "points to" (references) an instance of a reference type. Unlike pointers you can't perform arithmetic on them, and this is so the garbage collector can do its thing and track what objects are still referenced (so they are not collected).

So you are quite safe to set the UserData to an instance of the planet object. It will refer to that same object and a copy will not be made.

It's a different story if you have a value type (ie: struct Planet). This would get copied when you assign it. (Actually, in this case, because UserData is of type object and expects a reference type - the framework will "box" your value type by copying it into an immutable reference type.)

Personally I find it helpful - especially when doing game development where you care about memory - to use different syntax highlighting colours for reference types vs value types.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .