I am trying to create a little fun 3D game in XNA, but I am having some problems with scaling my models. I use models from random sites, so my battleship is for an example 10 times larger than my planet.
I thought I had found out how to scale my model, but it does not show up when I do, or the coordinates get really messed up. Is there a way to scale my models but still keep the same position system?
Here is my model base class.
public class BaseModel
{
protected Matrix world = Matrix.Identity;
protected Matrix RotationMatrix = Matrix.Identity;
protected Model model;
protected float scale = 1.0f;
protected Vector3 direction;
public Vector3 Position
{
get
{
return world.Translation;
}
set
{
world = Matrix.CreateTranslation(value);
}
}
public BaseModel(Model model, Vector3 position, float scale = 1)
: this(model, position, Vector3.Zero, Matrix.Identity, scale)
{
}
public BaseModel(Model model, Vector3 position, Vector3 direction, float scale = 1)
: this(model, position, direction, Matrix.Identity, scale)
{
}
public BaseModel(Model model, Vector3 position, Vector3 direction, Matrix rotationMatrix, float scale = 1)
{
this.model = model;
this.Position = position;
this.direction = direction;
this.RotationMatrix = rotationMatrix;
this.scale = scale;
}
public virtual void Update(GameTime gameTime)
{
world *= Matrix.CreateTranslation(direction);
}
public virtual void Draw(GameTime gameTime, Camera camera)
{
Matrix[] modelTransforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(modelTransforms);
Matrix wworld = GetWorld();
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = modelTransforms[mesh.ParentBone.Index] * wworld;
effect.View = camera.view;
effect.Projection = camera.projection;
}
mesh.Draw();
}
}
public virtual Matrix GetWorld()
{
return RotationMatrix * world * Matrix.CreateScale(scale);
}
public virtual Model GetModel()
{
return model;
}
public virtual bool CollidesWith(Model otherModel, Matrix otherWorld)
{
// Loop through each ModelMesh in both objects and compare
// all bounding spheres for collisions
foreach (ModelMesh myModelMeshes in model.Meshes)
{
foreach (ModelMesh hisModelMeshes in otherModel.Meshes)
{
if (myModelMeshes.BoundingSphere.Transform(
GetWorld()).Intersects(
hisModelMeshes.BoundingSphere.Transform(otherWorld)))
return true;
}
}
return false;
}
}
return Matrix.CreateScale(scale) * model;
inGetModel()
. \$\endgroup\$Model
is not a matrix. It's a XNA class that represents a collection of 3D meshes and bones. \$\endgroup\$world
variable has a misleading name, and from the context, I'd recommend changing it toTranslationMatrix
instead. \$\endgroup\$