Hallo!
Ich hoffe ihr könnt mir helfen. Ich habe mit XNA-game-studio(Windows Game) mir bisher eine Kamera die sich der Maus nachbewegt gebastelt und ich kann Objekte darstellen. Nun möchte ich eine Hand haben die sich der Kamera nachbewegt(so dass man sie im Bild sieht) aber irgendwie habe ich keine Ahnung wie ich das realisieren kann, vielleicht kann mir ja jemand helfen.Mit dem Gameobject "neu" hätte ich schon experimentiert aber bin nicht weiter gekommen.
Danke!
Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Destiny
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector3 cameraPosition;
Vector3 cameraTarget;
Vector3 cameraReference;
float aspectRatio;
float nearPlane;
float farPlane;
float fieldOfView;
Matrix Projection;
float cameraYaw;
float cameraPitch;
MouseState mStateCashe;
Gameobject neu;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
nearPlane = 0.1f;
farPlane = 1000.0f;
fieldOfView = 45.0f;
cameraPosition = new Vector3(0.0f, 0.0f, 0.0f);
cameraReference = new Vector3(0.0f, 0.0f, 1.0f);
cameraTarget = cameraReference + cameraPosition;
cameraYaw = 0.0f;
cameraPitch = 0.0f;
aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height;
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(fieldOfView), aspectRatio, nearPlane, farPlane);
Mouse.SetPosition(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);
mStateCashe = Mouse.GetState();
neu = new Gameobject();
neu.model = Content.Load<Model>("Models\\handnew");
neu.scale = 0.01f;
neu.position = new Vector3(0.5f, 0.5f, 0.5f);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
UpdateCamera(gameTime);
UpdateObject();
base.Update(gameTime);
}
public void UpdateCamera(GameTime gameTime)
{
KeyboardState kState = Keyboard.GetState();
MouseState mState = Mouse.GetState();
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
Vector3 moveVector = Vector3.Zero;
if (kState.IsKeyDown(Keys.W))
{
moveVector.Z += 3.0f * deltaTime;
}
if (kState.IsKeyDown(Keys.S))
{
moveVector.Z -= 3.0f * deltaTime;
}
if (kState.IsKeyDown(Keys.A))
{
moveVector.X += 3.0f * deltaTime;
}
if (kState.IsKeyDown(Keys.D))
{
moveVector.X -= 3.0f * deltaTime;
}
if (kState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
float mouseX = mState.X - mStateCashe.X;
float mouseY = mState.Y - mStateCashe.Y;
cameraPitch += (mouseY * 0.4f) * deltaTime;
cameraYaw -= (mouseX * 0.4f) * deltaTime;
cameraPitch = MathHelper.Clamp(cameraPitch, MathHelper.ToRadians(-89.9f), MathHelper.ToRadians(89.9f));
Mouse.SetPosition(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);
Matrix cameraViewRotationMatrix = Matrix.CreateRotationX(cameraPitch) * Matrix.CreateRotationY(cameraYaw);
Matrix cameraMoveRotationMatrix = Matrix.CreateRotationY(cameraYaw);
Vector3 transformedCameraReference = Vector3.Transform(cameraReference, cameraViewRotationMatrix);
cameraPosition += Vector3.Transform(moveVector, cameraMoveRotationMatrix);
cameraTarget = transformedCameraReference + cameraPosition;
base.Update(gameTime);
}
public void UpdateObject()
{
neu.position = cameraPosition + new Vector3(0.1f, -0.05f, 0.2f);
neu.rotation.Y=-1.2f;//-1.2f
neu.rotation.X=0.0f;//0.0f
neu.rotation.Z = 1.4f;//1.4f
neu.scale = 0.0004f;
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawGameObject(neu);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
void DrawGameObject(Gameobject gameobject)
{
foreach (ModelMesh mesh in gameobject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World =
Matrix.CreateFromYawPitchRoll(gameobject.rotation.Y,
gameobject.rotation.X,
gameobject.rotation.Z) * Matrix.CreateScale(gameobject.scale) *
Matrix.CreateTranslation(gameobject.position);
effect.Projection = Projection;
effect.View = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
}
mesh.Draw();
}
}
}
}
Gameobject.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Destiny
{
class Gameobject
{
public Model model = null;
public Vector3 position = Vector3.Zero;
public Vector3 rotation = Vector3.Zero;
public float scale = 1.0f;
}
}
Program.cs:
using System;
namespace Destiny
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}