Accessing a constraint's properties

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
run
Posts: 10
Joined: Thu Jun 02, 2011 8:36 am

Accessing a constraint's properties

Post by run »

Hi,

I've got a very simple scene setup for testing and learning constraints. 2 cubes, one's kinematic, the other's dynamic; connected by a ballsocket joint.

This works as expected, but I cannot read the ballsocket's RelativeVelocity property using a "get" accessor.

If I add a "get" block of code, other parts of the code, that were working fine, pop up errors like crazy.

I'm obviously doing some thing(s) wrong. I've checked the obvious things, curly brackets, semi-colons, etc. I've tried placing the "get" in "Update" rather
than "LoadContent" and varied what's happening in the block of code inself,.. a lot.

I've been through the demo scenes and the "get"'s that I've found there are somewhat complex, so that just muddies the water, and the generic examples
at MS and other online C# sites aren't specific enough to really help, so here I am.

Hopefully someone can provide me with, or point me to, a simple, but specific example of accessing a constraint's properties.

My knowledge of C#, xna and Bepu are obviously limited, so please be gentle; you can chuckle, but no guffaws :)

Thanks,

Jeff
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Accessing a constraint's properties

Post by Norbo »

I'm guessing that the ball socket joint's RelativeVelocity property was being put into something that was not a Vector3. For example:

Code: Select all

float thisFails = ballSocketJoint.RelativeVelocity;
Vector3 thisWorks = ballSocketJoint.RelativeVelocity;
The reason why it has to be a Vector3 is that the BallSocketJoint constraint type measures velocity in three dimensions. If you'd like the relative speed, then you can take the length of that three dimensional relative velocity.

The key to figuring these issues out is to figure out what the errors are trying to say. Once you're familiar with them, fixing them becomes pretty easy (especially compared to some of the errors various C++ compilers spit out :)).
run
Posts: 10
Joined: Thu Jun 02, 2011 8:36 am

Re: Accessing a constraint's properties

Post by run »

Vector3 was what I used initially, though I wasn't using a "Get{ }" accessor.
And then I tried the individual vector3 elements like this (after I created a BallSocket joint call bjoint)

Code: Select all

float vecx = bjoint.RelativeVelocity.X;
I've since tried lots of variations; with both the vector and the separate elements,
Things would compile, without errors, but the values were always zero for all elements.

The errors all come when I try to create a class and do a "get{ }" to try and access the ball joint's properties,
so that makes me think I'm basically not doing the C# stuff correctly.

The way I'm reading things I need to create a Class to access properties, is that correct? Or am going down the wrong path?

-Jeff
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Accessing a constraint's properties

Post by Norbo »

Things would compile, without errors, but the values were always zero for all elements.
That would be expected if the involved entities were not moving yet.

I would recommend focusing the the base programming language before getting into the physics engine; trying to do both simultaneously will just cause unnecessary pain at the very beginning. Charles Petzold has some free e-books on C# (http://www.charlespetzold.com). Spending some time on some bite-size/barebones starter applications, or fiddling with code designed for beginning educational purposes, would probably help too.

That said, the RelativeVelocity property is a getter. You don't have to create a class with your own getter property in it to retrieve the value from the RelativeVelocity property. In any function from which you can access a BallSocketJoint object somehow, you can get its relative velocity by just using its RelativeVelocity property.

Functions (methods) do live within classes (and technically structs too) in C#, though.
run
Posts: 10
Joined: Thu Jun 02, 2011 8:36 am

Re: Accessing a constraint's properties

Post by run »

Ahh, got it working; reading both as a vector and as individual components !

Once you explained that I didn't need to create a class, I removed the code that was confusing things and set it up in the way I thought it should work .
Then looked at the error messages and got " object reference not set to an instance of an object", popped that into Google.

And waddya know, I wasn't instantiating the floats and the vector3 I was trying to assign RelativeVelocity to.

I went through some beginner tutorials a month or 2 ago, much of which has drained out of my head because I haven't used it, but I kinda knew that stuff was
probably going come back to bite me, because I now remember thinking how redundant setting up variables seem to be. But there's nothing like a nice, figurative, face
plant to stick something in your long term memory :)

Thanks for the help and the links. It's much appreciated,

Jeff
Post Reply