Reputation: 11
I am trying to make a simple program that displays 3 different imported assets at different locations in the world. They are all rendering properly, but I can't figure out how to apply transformations to them to get them placed at different points in the world, how would I go about doing this? I will paste my code below, but please be kind as I just started learning MonoGame 3d a few days ago. Any other suggestions for optimization would be welcome as well.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SharpDX.Direct3D11;
using System.Collections.Generic;
namespace TestGame
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Camera
Vector3 camTarget;
Vector3 camPosition;
Matrix projectionMatrix;
Matrix viewMatrix;
Matrix worldMatrix;
Rectangle screenRectangle;
//Geometric info
Model ground;
Model cube;
Model sphere;
Texture texture;
Vector3 sphereCords;
Vector3 groundCords;
Vector3 cubeCords;
BasicEffect basicEffect;
List<Model> modelList = new List<Model>();
List<Texture> textureList = new List<Texture>();
//Orbit
bool orbit = false;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
//Screen Rectangle Setup
screenRectangle = new Rectangle(0, 0, 800, 480);
//Setup Camera
camTarget = new Vector3(0f, 0f, 0f);
camPosition = new Vector3(0f, 0f, -5);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45f), graphics.
GraphicsDevice.Viewport.AspectRatio,
1f, 1000f);
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
new Vector3(0f, 1f, 0f)); // Y up
worldMatrix = Matrix.CreateWorld(camTarget, Vector3.
Forward, Vector3.Up);
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
ground = Content.Load<Model>("Assets/Ground/Ground");
modelList.Add(ground);
cube = Content.Load<Model>("Assets/Cube/MonoCube");
modelList.Add(cube);
sphere = Content.Load<Model>("sphere");
sphereCords = new Vector3(0f, -5f, 0f);
modelList.Add(sphere);
texture = Content.Load<Texture>("Assets/Ground/DirtGround");
textureList.Add(texture);
texture = Content.Load<Texture>("Assets/Cube/MonocubeTexture");
textureList.Add(texture);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed || Keyboard.GetState().IsKeyDown(
Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
camPosition.X -= 0.1f;
camTarget.X -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
camPosition.X += 0.1f;
camTarget.X += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
camPosition.Y -= 0.1f;
camTarget.Y -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
camPosition.Y += 0.1f;
camTarget.Y += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemPlus))
{
camPosition.Z += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
{
camPosition.Z -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
orbit = !orbit;
}
if (orbit)
{
Matrix rotationMatrix = Matrix.CreateRotationY(
MathHelper.ToRadians(1f));
camPosition = Vector3.Transform(camPosition,
rotationMatrix);
}
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
Vector3.Up);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawModel(modelList[0], new Vector3(0, -5, 0));
DrawModel(modelList[1], new Vector3(0, 5, 0));
base.Draw(gameTime);
}
void DrawModel(Model model, Vector3 vector3)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
//effect.EnableDefaultLighting();
effect.AmbientLightColor = new Vector3(1f, 0, 0);
effect.View = viewMatrix;
effect.World = worldMatrix;
effect.Projection = projectionMatrix;
}
mesh.Draw();
}
}
}
}
I have tried creating Vector3 variables for each object, but cannot seem to apply them as a transformation.
Upvotes: 1
Views: 32