| Poster | : h0schi | | Posts | : 6 | | Country | : Germany | | City | : Dortmund |
| | | | Posted by h0schi on 09/01/2008 at 14:02:57
| | Hi there,
can anyone post me in the direction of creating a camera moving around an object ?.
let me say. i got an ocean and a ship on it. i want the camera move around the ship. so you can see from all sides.
i tryed rotating Camera with Quaternion but cant make it work.
Any hints or Links would be very nice.
Thx guys.
And by the way, this tuts are great!. | |
|
|
| |
| |
| Poster | : tweety | | Posts | : 23 | | Country | : Austria | | City | : Hallein |
| | | | Posted by tweety on 09/01/2008 at 15:21:01
| | Hi there!
I´ve made a short Windows Game for you.
Additionally you will need
a .x file (I think you will use your ship)
a texture file (I use the clouds.jpg) and
the "Series4Effects.fx" file.
You can get all these items from the Project4 download.
The solution for problem is to move the camera in a circle around your ship. Then set the "LookAt" at your ship....DONE!
try this code:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace WindowsGame7
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
private GraphicsDevice device;
private Effect effect;
Model FirstCube = new Model();
private Matrix viewMatrix;
private Matrix projectionMatrix;
private int degrees;
private Texture2D CubeTexture;
Vector3 center = new Vector3(0, 0, 0);
private Model FillModelFromFile(string asset)
{
Model mod = content.Load<Model>(asset);
foreach (ModelMesh modmesh in mod.Meshes)
foreach (ModelMeshPart modmeshpart in modmesh.MeshParts)
modmeshpart.Effect = effect.Clone(device);
return mod;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{
SetUpXNADevice();
FirstCube = FillModelFromFile("Cube");
CubeTexture = content.Load<Texture2D>("clouds");
base.Initialize();
}
private void SetUpXNADevice()
{
device = graphics.GraphicsDevice;
graphics.PreferredBackBufferWidth = 750;
graphics.PreferredBackBufferHeight = 750;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
//Window.Title = "";
effect = content.Load<Effect>("Series4Effects");
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateCamera();
base.Update(gameTime);
}
private void UpdateCamera()
{
//let the camera look at the center (also the position of the cube)
center = new Vector3(0, 0, 0);
float radius = 300;
float altitude = 50;
//calculate campos by using the 2d formula: x=r*cos(degrees) y=r*sin(degrees)
Vector3 campos = new Vector3((float)(radius * Math.Cos((Math.PI/180)*degrees)), (float)(radius * Math.Sin((Math.PI/180)*degrees)), altitude);
//add 1 degree per frame
degrees++;
Vector3 camup = new Vector3(0, 0, 1);
viewMatrix = Matrix.CreateLookAt(campos, center, camup);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, ((float)this.Window.ClientBounds.Width) / ((float)this.Window.ClientBounds.Height), 1.0f, 1000.0f);
}
private void DrawFirstCube()
{
Matrix worldMatrix = Matrix.CreateScale(0.1f) * Matrix.CreateTranslation(center);
foreach (ModelMesh modmesh in FirstCube.Meshes)
{
foreach (Effect currenteffect in modmesh.Effects)
{
currenteffect.Parameters["xEnableLighting"].SetValue(true);
currenteffect.Parameters["xLightDirection"].SetValue(new Vector3(100, 0, 0));
currenteffect.Parameters["xAmbient"].SetValue(0.8f);
currenteffect.CurrentTechnique = currenteffect.Techniques["Textured"];
currenteffect.Parameters["xWorld"].SetValue(worldMatrix);
currenteffect.Parameters["xView"].SetValue(viewMatrix);
currenteffect.Parameters["xProjection"].SetValue(projectionMatrix);
currenteffect.Parameters["xTexture"].SetValue(CubeTexture);
}
modmesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
DrawFirstCube();
base.Draw(gameTime);
}
}
}
|
Please inform me if this was helpful!
Grüße aus Österreich
Tweety | |
|
|
| |
| |
| Poster | : h0schi | | Posts | : 6 | | Country | : Germany | | City | : Dortmund |
| | | | Posted by h0schi on 10/01/2008 at 04:15:16
| | Thx, iam at work now, but give it a try later.
Schönen Gruß nach Österreich. | |
|
|
| |
| |
| Poster | : h0schi | | Posts | : 6 | | Country | : Germany | | City | : Dortmund |
| | | | Posted by h0schi on 10/01/2008 at 05:43:37
| | Andere Frage, muss ich die Projectionsmatrix jedesmal neu setzen ? Normal reicht doch einmal oder ?
Danke | |
|
|
| |
| |
| Poster | : h0schi | | Posts | : 6 | | Country | : Germany | | City | : Dortmund |
| | | | Posted by h0schi on 10/01/2008 at 05:44:24
| | Andere Frage, muss ich die Projectionsmatrix jedesmal neu setzen ? Normal reicht doch einmal oder ?
Danke | |
|
|
| |
| |
| Poster | : tweety | | Posts | : 23 | | Country | : Austria | | City | : Hallein |
| | | | Posted by tweety on 10/01/2008 at 10:40:15
| | Hi!
Natürlich reicht es die Projektionsmatrix nur einmal zu setzen...gut beobachtet! =)
------------------------------------------------
You´re right. You do not need to reset the ProjectionMatrix during the Update method... god job!
Tweety | |
|
|
| |
| |
| Poster | : h0schi | | Posts | : 6 | | Country | : Germany | | City | : Dortmund |
| | | | Posted by h0schi on 10/01/2008 at 12:00:25
| | Hi,
ich schreib mal in englisch damit die anderen es auch verstehen hier.
I tried the example but the camera moves around and the object also is rotation.
Example.
A ship move from right to left..
<-------> Ship
i dont want that the ship itself rotates. like this. I mean the view to that ship. ship of course stays at his world coordinates. but the look is wrong.
<
\
\
\
>
It stays the same all time. moving from left to right.
The camera should then be able to move around that ship, but ship stays straight <------>.
so i can see the front. the other side of ship. back etc...
Many thx again for helping.
| |
|
|
| |
| |
| Poster | : tweety | | Posts | : 23 | | Country | : Austria | | City | : Hallein |
| | | | Posted by tweety on 10/01/2008 at 12:42:32
| | I´m sorry...but I can´t get the problem properly...
ship....shouldn´d move
camera...should move around the ship (cirle?, ellipse?)
or did I get this wrong...
In fact in this example the cube/model isn´t moving at all... the camera is rotating around a Vector/Point(0,0,0) called "center". It only seem that the cube is rotating...
Try to replace this vector with the shipcenter:
e.g. center = shipcenter; (use the correct
Vector3 of course)
instead of center = new Vector3(0,0,0);
if you still have problems let me know...
you also can mail me your project to:
dirty_fighting<at>gmx<dot>at
I´ll have a look at it if you want
greetz
tweety
| |
|
|