Reversing LinearVelocity and LinearMomentum

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
vintagegames
Posts: 19
Joined: Wed Dec 16, 2009 4:36 pm

Reversing LinearVelocity and LinearMomentum

Post by vintagegames »

I have a C# Class object called "Slot" that basically contains a MaxVector3 and MinVector3. All other objects that inherit this "Slot" object are constrained to motion along a line connecting these two Vector3 objects.

When a collision with one of the endpoints of the "Slot" object happens, I want to effectively reverse the linearVelocity and linearMomentum so that the item will reverse back down the Slot in the opposite direction.

I have created two C# methods to accomplish this, but neither of them is able to override the Entity.linearVelocity value (even if I set Entity.linearVelocity = Vector3.Zero). No clue why I can't override these values with their negative equivalents?

Code: Select all

private Vector3 ReverseLinearMomentum(Vector3 currentLM)
        {
            Vector3 newLM = new Vector3();
            if (Math.Abs(currentLM.X) > .5)
            {
                newLM.X = -(currentLM.X);
            }
            if (Math.Abs(currentLM.Y) > .5)
            {
                newLM.Y = -(currentLM.Y);
            }
            if (Math.Abs(currentLM.Z) > .5)
            {
                newLM.Z = -(currentLM.Z);
            }
            return newLM;
        }

        private Vector3 ReverseLinearVelocity(Vector3 currentLV)
        {
            Vector3 newLV = new Vector3();
            if (Math.Abs(currentLV.X) > .5)
            {
                newLV.X = -(currentLV.X);
            }
            if (Math.Abs(currentLV.Y) > .5)
            {
                newLV.Y = -(currentLV.Y);
            }
            if (Math.Abs(currentLV.Z) > .5)
            {
                newLV.Z = -(currentLV.Z);
            }
            return newLV;
        }
Usage Example:

Code: Select all

Vector3 beforeV = se.DisplayModel.Entity.linearVelocity;
se.DisplayModel.Entity.linearVelocity = ReverseLinearVelocity(beforeV);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Reversing LinearVelocity and LinearMomentum

Post by Norbo »

I think what you might be encountering is the buffered nature of the linearVelocity field. Most of the entity properties that lack an 'internal' prefix (linearVelocity as opposed to internalLinearVelocity) are buffered for thread safety when the entity belongs to a space. Properties which are buffered say so in their intellisense comments.

Basically, writes to a buffered field while the entity is owned by a space are accumulated and finalized at the beginning of the next frame. You won't be able to immediately see the change.
vintagegames
Posts: 19
Joined: Wed Dec 16, 2009 4:36 pm

Re: Reversing LinearVelocity and LinearMomentum

Post by vintagegames »

I appreciate the explanation, but how can I go about reversing the LinearVelocity or LinearMomentum of an Entity that is in motion?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Reversing LinearVelocity and LinearMomentum

Post by Norbo »

Just setting it should work. For buffered fields, it'll take a frame to take effect. For a field like internalLinearVelocity, it will be instant. If you're not accessing it asynchronously, you can use internalLinearVelocity safely.

Like you have in the code sample, just doing something like:

Code: Select all

entity.linearVelocity = -entity.linearVelocity
will do it.
vintagegames
Posts: 19
Joined: Wed Dec 16, 2009 4:36 pm

Re: Reversing LinearVelocity and LinearMomentum

Post by vintagegames »

Still not getting anywhere. I'm even getting some weird behavior where linearVelocity might be (-2000,0,0) but internalLinearVelocity might be (2000,0,0) at the same time. My Entity simply vibrates within a very small (maybe .5f units) space back and forth when it reaches the end of my "Slot" obj.

Here is a .zip with screenshots of my debugger as I step through values line-by-line. (It includes 10 separate screenshots for 2 consecutive passes through Update() method so all variables and values can be seen)

Any ideas?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Reversing LinearVelocity and LinearMomentum

Post by Norbo »

The different values for linearVelocity and internalLinearVelocity can be explained by the fact that one is buffered, and the other is not. Internal linear velocity will show you the current, unbuffered linear velocity. linearVelocity will show you the linear velocity as of the last read buffer update.

If you're still using the same code as above, the reason your object gets stuck at the end of the slot is that you never go below a magnitude of 0.5 on your velocity once it is reached. It goes negative, but it doesn't decrease in magnitude. Every single method call afterwards, the velocity will flip.
Post Reply