Rendering a complex model that exists as a physics object

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Rendering a complex model that exists as a physics object

Post by mcmonkey »

So, I got cuboids (boxes) rendering /perfectly/ thanks to previous posts. They rotate correctly and all.

I'm now trying to handle a complex model generated from a file.
The origin of the model is at the model's bottom, and obviously that's not where the center of mass is, so I record the offset generated by the model and that all works well.

I render essentially with "offset * Transform" where "transform" is the exact same calculations I used to render cuboids. Solid, right? Nope! The monster I've created insists on floating and/or going inside the floor and just all around rotating on definitely not anything logical.

My code:

Code: Select all

        public override void SpawnBody()
        {
            model = TheClient.Models.GetModel(mod);
            Shape = TheClient.Models.Handler.MeshToBepu(model.OriginalModel);
            Assimp.Matrix4x4 mat = model.OriginalModel.RootNode.Transform;
            offset = Location.FromBVector(Shape.Position);
            Matrix4 root = new Matrix4(mat.A1, mat.A2, mat.A3, mat.A4, mat.B1, mat.B2, mat.B3, mat.B4, mat.C1, mat.C2, mat.C3, mat.C4, mat.D1, mat.D2, mat.D3, mat.D4);
            transform = Matrix4.CreateTranslation(offset.ToOVector()) * root;
            base.SpawnBody();
        }

        public override void Render()
        {
            Matrix4 orient = GetOrientationMatrix();
            Matrix4 mat = transform * (Matrix4.CreateScale(scale.ToOVector()) * orient * Matrix4.CreateTranslation((GetPosition()).ToOVector()));
            GL.UniformMatrix4(2, false, ref mat);
            TheClient.Rendering.SetMinimumLight(0.0f);
            model.Draw();
        }
Note that I'm 70% sure "Matrix4 root = ..." is just an identity matrix in this case. (Transposing it and stuff like that doesn't seem to change how the scene renders at all).
Also note that Scale = (1, 1, 1).
GetPosition() returns the BEPU entity's position.
The variable "transform" is not touched outside this code.

I'm going to assume you're capable of picture "rotating wrong" and not post images of flying and/or half-submerged poorly-modeled corpses.
I can find a model that's less terribly ugly and get screenshots/gifs if it would be helpful.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Rendering a complex model that exists as a physics objec

Post by Norbo »

It sounds like shape recentering isn't taken into account properly. All collision shapes are centered on their shape-constructor-computed center of mass. The offset taken from the file isn't enough, because the modeling software isn't considering the center of mass in general.

Check out the shape recentering documentation for more information.

(I do see some things in the posted code which may imply that you are trying to handle shape recentering already; if you did and it's still flying around, then it's probably a sign issue with an offset or something. For example, if Shape.Position still contains the constructor-computed center of mass of the shape, then there's a missing negation.)
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Rendering a complex model that exists as a physics objec

Post by mcmonkey »

So I figured out what was actually happening... the clientside representation of the object had a mass of 0, while the serverside had a mass of 100.

It was rendering fine, just the entity itself was rotating weirdly.

The server sends updates every tick on the position/velocity/angle/angular_velocity of the entity, as long as the entity is active, and once at deactivation time.

For whatever reason, after the entity deactivated on the server, and the server sent a final packet theoretically telling the client that the entity is now at (0,0,0) for all properties, the object continued to rotate.

Is it possible a deactivated object can still have an AngularVelocity > 0? That's my best guess at why the entity rotated regardless.

I guess it's normal for mass:0 objects to go inside each other, though it seems odd to me.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Rendering a complex model that exists as a physics objec

Post by Norbo »

Is it possible a deactivated object can still have an AngularVelocity > 0? That's my best guess at why the entity rotated regardless.
Yes, deactivated objects can have a small bit of velocity below the deactivation threshold remaining. The simulation keeps that small bit of velocity around for continuity purposes. In complicated interacting structures like stacks, zeroing out the velocities would cause a visible jerk on awakening as the constraints adapt to subtly different conditions.

Further, the deactivation rules for kinematic objects are different. Any velocity whatsoever will keep them active. (That's because nothing except direct intervention can change a kinematic entity's velocities, so it is assumed that small nonzero velocities are intentional.)
I guess it's normal for mass:0 objects to go inside each other, though it seems odd to me.
Yes, kinematic entities will go through other objects. While an object constructed with zero (or negative, or NaN, or infinite) mass will end up kinematic and have Mass and InverseMass values of 0, their effective mass is infinite. No physical force, including collisions, can change their motion.
Post Reply