Skip to content

CompoundShape Scaling #66

@Andy16823

Description

@Andy16823

Hello, i try to create an compoundshape for an assimp scene. Everything works fine when the scale is equal on every axis. So 1,1,1 and 2,2,2 works correct. However when i try to scale it to something like 1,2,1 its not correct anymore at all. Here is my code

using Assimp;
using BulletSharp;
using Genesis.Core.GameElements;
using Genesis.Math;
using Genesis.Physics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Genesis.Core.Behaviors.Physics3D
{
    public class CompoundMeshCollider2 : ColliderBehavior3D
    {
        public CompoundMeshCollider2(PhysicHandler physicHandler) : base(physicHandler)
        {
        }

        public void CreateCollider(String path, int collisionGroup = -1, int collisionMask = -1)
        {
            var element = (Element3D)this.Parent;
            var compoundShape = new CompoundShape(false);



            var btTranslation = BulletSharp.Math.Matrix.Translation(element.Location.ToBulletVec3());
            var btScale = BulletSharp.Math.Matrix.Scaling(1.0f);
            var btRotation = BulletSharp.Math.Matrix.RotationX(element.Rotation.X) * BulletSharp.Math.Matrix.RotationY(element.Rotation.Y) * BulletSharp.Math.Matrix.RotationZ(element.Rotation.Z);
            var btStartTransform = btTranslation * btRotation * btScale;

            Assimp.AssimpContext importer = new Assimp.AssimpContext();
            //importer.SetConfig(new Assimp.Configs.NormalSmoothingAngleConfig(66.0f));
            var model = importer.ImportFile(path , Assimp.PostProcessPreset.TargetRealTimeQuality | Assimp.PostProcessSteps.Triangulate);

            foreach (var mesh in model.Meshes)
            {
                int[] indicies = mesh.GetIndices();
                float[] verticies = mesh.Vertices.SelectMany(v => new float[] {v.X, v.Y, v.Z}).ToArray();
                var meshScale = BulletSharp.Math.Matrix.Scaling(element.Size.ToBulletVec3());
                var node = CompoundMeshCollider2.FindMeshNode(model.RootNode, mesh.Name);
                var transform = CompoundMeshCollider2.GetMatrix(model.RootNode.Transform) * CompoundMeshCollider2.GetMatrix(node.Transform);

                TriangleIndexVertexArray triangle = new TriangleIndexVertexArray(indicies, verticies);
                BvhTriangleMeshShape shape = new BvhTriangleMeshShape(triangle, true);
                shape.LocalScaling = new BulletSharp.Math.Vector3(1, 1, 1);
                compoundShape.AddChildShape(transform, shape);
            }

            compoundShape.LocalScaling = TransformVector(element.Size);
            compoundShape.CalculateLocalInertia(0);

            Collider = new BulletSharp.CollisionObject();
            Collider.CollisionShape = compoundShape;
            Collider.UserObject = this.Parent;
            Collider.WorldTransform = btStartTransform;
            PhysicHandler.ManageElement(this, collisionGroup, collisionMask);
        }

        public override void CreateCollider(int collisionGroup = -1, int collisionMask = -1)
        {
            throw new NotImplementedException();
        }

        private static Node FindMeshNode(Node root, String meshname)
        {
            return root.FindNode(meshname);
        }

        private static BulletSharp.Math.Matrix GetMatrix(Assimp.Matrix4x4 matrix)
        {
            return new BulletSharp.Math.Matrix(
                matrix.A1, matrix.B1, matrix.C1, matrix.D1, // Erste Spalte
                matrix.A2, matrix.B2, matrix.C2, matrix.D2, // Zweite Spalte
                matrix.A3, matrix.B3, matrix.C3, matrix.D3, // Dritte Spalte
                matrix.A4, matrix.B4, matrix.C4, matrix.D4  // Vierte Spalte
            );
        }

        private static BulletSharp.Math.Vector3 TransformVector(Vec3 vec)
        {
            return new BulletSharp.Math.Vector3(vec.X, vec.Y, vec.Z);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions