Jump to content

  • Log In with Google      Sign In   
  • Create Account


granberg

Member Since 28 Apr 2011
Offline Last Active Sep 23 2011 10:59 AM

Topics I've Started

Make cube

23 September 2011 - 10:43 AM

Hi everybody,I had a problem want to someguys show help,you know I working with XNA primitive,I want to build a cube with six side,I already done it,but it can not rotate along with axes(X,y,z),I don't know is I should use orbit Rotate or just rotate?show me some code please!as soon as answer me!

Making an airplane

22 September 2011 - 10:57 AM

Hi body,here I'm deal a difficult problem with my XNA programm,I'm going to let a ariplane that is form by XNA primitive to following along the Y and Z axis,but it can not change to the propely direction,and even the propeller is always far from it,does someone knowing the slove way?

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;
using CameraViewer;

namespace MGHGame
{
/// <summary>
/// This is the driving class for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//------------------------------------------------------------
// C L A S S L E V E L D E C L A R A T I O N S
//------------------------------------------------------------
// constant definitions
private const float BOUNDARY = 16.0f;

// accesses drawing methods and properties
GraphicsDeviceManager graphics;

// handle mouse on the PC
#if !XBOX
MouseState mouse;
#endif

// for loading and drawing 2D images on the game window
SpriteBatch spriteBatch;

// load and access PositionColor.fx shader
private Effect positionColorEffect; // shader object
private EffectParameter positionColorEffectWVP; // to set display matrix for window

// load and access Texture.fx shader
private Effect textureEffect; // shader object
private EffectParameter textureEffectWVP; // cumulative matrix w*v*p
private EffectParameter textureEffectImage; // texture parameter

// camera
private Camera cam = new Camera();

// vertex types and buffers
private VertexDeclaration positionColor;
private VertexDeclaration positionColorTexture;

// ground vertices and texture
VertexPositionColorTexture[]
groundVertices = new VertexPositionColorTexture[4];
private Texture2D grassTexture;

VertexPositionColor[] airplaneVertices = new VertexPositionColor[3];
VertexPositionColor[] propellerVertices = new VertexPositionColor[4];

float propellerSpin;
Vector3 speed;
Vector3 airplanePosition = new Vector3(0.0f, 0.75f, -8.0f);

/// <summary>
/// Initializes: -GraphicsDeviceManager object for drawing
/// -ContentManager object for loading media
/// </summary>
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// This method is called when the program begins to set game application
/// properties such as status bar title and draw mode. It initializes the
/// camera viewer projection, vertex types, and shaders.
/// </summary>
private void InitializeBaseCode()
{
// set status bar in PC Window (there is none for the Xbox 360)
Window.Title = "Microsoft?XNA Game Studio Creator's Guide, Second Edition";

// see both sides of objects drawn
graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;

// set camera matrix
cam.SetProjection(Window.ClientBounds.Width,
Window.ClientBounds.Height);

// initialize vertex types
positionColor = new VertexDeclaration(graphics.GraphicsDevice,
VertexPositionColor.VertexElements);
positionColorTexture = new VertexDeclaration(graphics.GraphicsDevice,
VertexPositionColorTexture.VertexElements);

// load PositionColor.fx and set global params
positionColorEffect = Content.Load<Effect>("Shaders\\PositionColor");
positionColorEffectWVP = positionColorEffect.Parameters["wvpMatrix"];

// load Texture.fx and set global params
textureEffect = Content.Load<Effect>("Shaders\\Texture");
textureEffectWVP = textureEffect.Parameters["wvpMatrix"];
textureEffectImage = textureEffect.Parameters["textureImage"];
}

private void InitializeAirplaneBody()
{
Vector3 position;
Color color = Color.Orange;
position = new Vector3(0.0f, 0.0f, 0.0f); // lower front
airplaneVertices[0] = new VertexPositionColor(position, color);

position = new Vector3(0.5f, 0.5f, 0.0f); // top back
airplaneVertices[1] = new VertexPositionColor(position, color);

position = new Vector3(0.5f, -0.0f, 0.0f); // lower back
airplaneVertices[2] = new VertexPositionColor(position, color);
}

private void InitializePropeller()
{
Vector3 position;
Color color = Color.LightBlue;
position = new Vector3(-0.5f, 0.05f, 0.0f); // top left
propellerVertices[0] = new VertexPositionColor(position, color);
position = new Vector3(-0.5f, -0.05f, 0.0f);// lower left
propellerVertices[1] = new VertexPositionColor(position, color);
position = new Vector3(0.5f, 0.05f, 0.0f); // top right
propellerVertices[2] = new VertexPositionColor(position, color);
position = new Vector3(0.5f, -0.05f, 0.0f); // lower right
propellerVertices[3] = new VertexPositionColor(position, color);
}

void InitializeSpeed()
{
Random randomNumber = new Random();
speed.Y = -1.0f - randomNumber.Next(3);
speed.Z = -1.0f - randomNumber.Next(3);
}

/// <summary>
/// Set vertices for rectangular surface that is drawn using a triangle strip.
/// </summary>
private void InitializeGround(){
const float BORDER = BOUNDARY;
Vector2 uv = new Vector2(0.0f, 0.0f);
Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f);
Color color = Color.White;

// top left
uv.X= 0.0f; uv.Y= 0.0f; pos.X=-BORDER; pos.Y=0.0f; pos.Z=-BORDER;
groundVertices[0] = new VertexPositionColorTexture(pos, color, uv);

// bottom left
uv.X= 0.0f; uv.Y=10.0f; pos.X=-BORDER; pos.Y=0.0f; pos.Z= BORDER;
groundVertices[1] = new VertexPositionColorTexture(pos, color, uv);

// top right
uv.X=10.0f; uv.Y= 0.0f; pos.X= BORDER; pos.Y=0.0f; pos.Z=-BORDER;
groundVertices[2] = new VertexPositionColorTexture(pos, color, uv);

// bottom right
uv.X=10.0f; uv.Y=10.0f; pos.X= BORDER; pos.Y=0.0f; pos.Z= BORDER;
groundVertices[3] = new VertexPositionColorTexture(pos, color, uv);
}

/// <summary>
/// Executes set-up routines when program begins.
/// </summary>
protected override void Initialize()
{
InitializeBaseCode();
InitializeGround();

InitializeAirplaneBody();
InitializePropeller();
InitializeSpeed();

base.Initialize();
}

/// <summary>
/// Draws colored surfaces with PositionColor.fx shader.
/// </summary>
/// <param name="primitiveType">Object type drawn with vertex data.</param>
/// <param name="vertexData">Array of vertices.</param>
/// <param name="numPrimitives">Total primitives drawn.</param>
private void PositionColorShader(PrimitiveType primitiveType,
VertexPositionColor[] vertexData,
int numPrimitives){
positionColorEffect.Begin(); // begin using PositionColor.fx
positionColorEffect.Techniques[0].Passes[0].Begin();

// set drawing format and vertex data then draw primitive surface
graphics.GraphicsDevice.VertexDeclaration = positionColor;
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
primitiveType, vertexData, 0, numPrimitives);

positionColorEffect.Techniques[0].Passes[0].End();
positionColorEffect.End(); // stop using PositionColor.fx
}

/// <summary>
/// Draws textured primitive objects using Texture.fx shader.
/// </summary>
/// <param name="primitiveType">Object type drawn with vertex data.</param>
/// <param name="vertexData">Array of vertices.</param>
/// <param name="numPrimitives">Total primitives drawn.</param>
private void TextureShader(PrimitiveType primitiveType,
VertexPositionColorTexture[] vertexData,
int numPrimitives){
textureEffect.Begin(); // begin using Texture.fx
textureEffect.Techniques[0].Passes[0].Begin();

// set drawing format and vertex data then draw surface
graphics.GraphicsDevice.VertexDeclaration = positionColorTexture;
graphics.GraphicsDevice.DrawUserPrimitives
<VertexPositionColorTexture>(
primitiveType, vertexData, 0, numPrimitives);

textureEffect.Techniques[0].Passes[0].End();
textureEffect.End(); // stop using Textured.fx
}

private void DrawAirplaneBody()
{
// 1: declare matrices
Matrix world, translation, rotationY;

// 2: initialize matrices
translation = Matrix.CreateTranslation(airplanePosition);
rotationY = Matrix.CreateRotationX(RotationAngle());

// 3: build cumulative world matrix using I.S.R.O.T. sequence
// identity, scale, rotate, orbit(translate & rotate), translate
world = translation;

// 4: set shader parameters
positionColorEffectWVP.SetValue(world * cam.viewMatrix
* cam.projectionMatrix);

// 5: draw object - primitive type, vertices, total primitives
PositionColorShader(PrimitiveType.TriangleStrip, airplaneVertices, 1);
}

/// <summary>
/// Triggers drawing of ground with texture shader.
/// </summary>
private void DrawGround()
{
// 1: declare matrices
Matrix world, translation;

// 2: initialize matrices
translation = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f);

// 3: build cumulative world matrix using I.S.R.O.T. sequence
// identity, scale, rotate, orbit(translate & rotate), translate
world = translation;

// 4: set shader parameters
textureEffectWVP.SetValue(world * cam.viewMatrix * cam.projectionMatrix);
textureEffectImage.SetValue(grassTexture);

// 5: draw object - primitive type, vertex data, # primitives
TextureShader(PrimitiveType.TriangleStrip, groundVertices, 2);
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// create SpriteBatch object for drawing animated 2D images
spriteBatch = new SpriteBatch(GraphicsDevice);

// load texture
grassTexture = Content.Load<Texture2D>("Images\\grass");
}

/// <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>
/// Updates camera viewer in forwards and backwards direction.
/// </summary>
float Move()
{
KeyboardState kb = Keyboard.GetState();
GamePadState gp = GamePad.GetState(PlayerIndex.One);
float move = 0.0f;
const float SCALE = 1.50f;

// gamepad in use
if (gp.IsConnected)
{
// left stick shifted left/right
if (gp.ThumbSticks.Left.Y != 0.0f)
move = (SCALE * gp.ThumbSticks.Left.Y);
}
// no gamepad - use UP&DOWN or W&S
else
{
#if !XBOX
if (kb.IsKeyDown(Keys.Up) || kb.IsKeyDown(Keys.W))
move = 1.0f; // Up or W - move ahead
else if (kb.IsKeyDown(Keys.Down) || kb.IsKeyDown(Keys.S))
move = -1.0f; // Down or S - move back
#endif
}
return move;
}

/// <summary>
/// Updates camera viewer in sideways direction.
/// </summary>
float Strafe()
{
KeyboardState kb = Keyboard.GetState();
GamePadState gp = GamePad.GetState(PlayerIndex.One);

// using gamepad leftStick shifted left / right for strafe
if (gp.IsConnected)
{
if (gp.ThumbSticks.Left.X != 0.0f)
return gp.ThumbSticks.Left.X;
}
// using keyboard - strafe with Left&Right or A&D
else if (kb.IsKeyDown(Keys.Left) || kb.IsKeyDown(Keys.A))
return -1.0f; // strafe left
else if (kb.IsKeyDown(Keys.Right) || kb.IsKeyDown(Keys.D))
return 1.0f; // strafe right
return 0.0f;
}

/// <summary>
/// Changes camera viewing angle.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
Vector2 ChangeView(GameTime gameTime)
{
const float SENSITIVITY = 250.0f;
const float VERTICAL_INVERSION = -1.0f; // vertical view control
// negate to reverse

// handle change in view using right and left keys
KeyboardState kbState = Keyboard.GetState();
int widthMiddle = Window.ClientBounds.Width/2;
int heightMiddle = Window.ClientBounds.Height/2;
Vector2 change = Vector2.Zero;
GamePadState gp = GamePad.GetState(PlayerIndex.One);

if (gp.IsConnected == true) // gamepad on PC / Xbox
{
float scaleY = VERTICAL_INVERSION*(float)
gameTime.ElapsedGameTime.Milliseconds/50.0f;
change.Y = scaleY * gp.ThumbSticks.Right.Y * SENSITIVITY;
change.X = gp.ThumbSticks.Right.X * SENSITIVITY;
}
else
{
// use mouse only (on PC)
#if !XBOX
float scaleY = VERTICAL_INVERSION*(float)
gameTime.ElapsedGameTime.Milliseconds/100.0f;
float scaleX = (float)gameTime.ElapsedGameTime.Milliseconds/400.0f;

// get cursor position
mouse = Mouse.GetState();

// cursor not at center on X
if (mouse.X != widthMiddle)
{
change.X = mouse.X - widthMiddle;
change.X /= scaleX;
}
// cursor not at center on Y
if (mouse.Y != heightMiddle)
{
change.Y = mouse.Y - heightMiddle;
change.Y /= scaleY;
}
// reset cursor back to center
Mouse.SetPosition(widthMiddle, heightMiddle);
#endif
}
return change;
}

void UpdateAirplanePosition(GameTime gameTime)
{
// change corresponding speed if beyond world抯 X and Z boundaries
if (airplanePosition.Y > BOUNDARY || airplanePosition.Y < -BOUNDARY)
speed.X *= -1.0f;
if (airplanePosition.Z > BOUNDARY || airplanePosition.Z < -BOUNDARY)
speed.Z *= -1.0f;

// increment position by speed * time scale between frames
float timeScale = gameTime.ElapsedGameTime.Milliseconds/1000.0f;
airplanePosition.Y -= speed.Y * timeScale;
airplanePosition.Z += speed.Z * timeScale;
}

float RotationAngle()
{
return (float)Math.Atan2((double)speed.Y, (double)speed.Z);

}

/// <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
KeyboardState kbState = Keyboard.GetState();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
|| kbState.IsKeyDown(Keys.Escape))
{
this.Exit();
}

// update camera
cam.SetFrameInterval(gameTime);
cam.Move(Move());
cam.Strafe(Strafe());
cam.SetView(ChangeView(gameTime));

UpdateAirplanePosition(gameTime);
base.Update(gameTime);
}

private void DrawPropeller(GameTime gameTime)
{
// 1: declare matrices
Matrix world, translation, orbitTranslate, orbitRotateY, rotationZ;

// 2: initialize matrices
// continous rotation - restrict it between 0 and 2pi
propellerSpin += gameTime.ElapsedGameTime.Milliseconds/50.0f;
propellerSpin = propellerSpin % (MathHelper.Pi * 2.0f);
rotationZ = Matrix.CreateRotationZ(propellerSpin);
orbitTranslate = Matrix.CreateTranslation(0.0f, -0.35f, 0.5f);
orbitRotateY = Matrix.CreateRotationX(RotationAngle());
translation = Matrix.CreateTranslation(airplanePosition.X + 0.4f,airplanePosition.Y + 0.1f, airplanePosition.Z);

// 3: build cumulative world matrix using I.S.R.O.T. sequence
// identity, scale, rotate, orbit(translate & rotate), translate
world = rotationZ * orbitTranslate * orbitRotateY * translation;

// 4: set shader parameters
positionColorEffectWVP.SetValue(world * cam.viewMatrix
* cam.projectionMatrix);

// 5: draw object - primitive type, vertices, # of primitives
PositionColorShader(PrimitiveType.TriangleStrip, propellerVertices, 2);
}

/// <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);

DrawGround();
DrawAirplaneBody();
DrawPropeller(gameTime);
base.Draw(gameTime);
}
}
}



Transform

09 September 2011 - 01:01 AM

Hi guys,could you give me a hand ,now I trapped in the XNA transform problem,I write a simply demo about a helicopter fly with a propeller and rotor,now the problem is I'm going to change the Y value of the helicopter also change the propeller and rotor,after I done it,the strage thing appear rotor and propeller didn't fowllow the direction
of helicoper,even the rotor is far away from helicopter!How could I do?

Transform

07 September 2011 - 10:59 AM

Hi guys,could you give me a hand ,now I trapped in the XNA transform problem,I write a simply demo about a helicopter fly with a propeller and rotor,now the problem is I'm going to change the Y value of the helicopter also change the propeller and rotor,after I done it,the strage thing appear rotor and propeller didn't fowllow the direction
of helicoper,even the rotor is far away from helicopter!How could I do?

render 3D primitive

28 August 2011 - 05:45 AM

Hi guys!I have been render two 3D graphics on my project,but I hope they can display together with the 2D sprite together,but unfortunately if I write spriteBatch.Begin(SpriteBlendMode.AlphaBlend); // start drawing 2D images

spriteBatch.Draw(rockTexture, rockPosition, null, Color.White,
rockRotation, rockCenter, 1.0f, SpriteEffects.None, 0.0f);
spriteBatch.Draw(shipTexture, shipPosition, null, Color.White,
shipRotation, shipCenter, 1.0f, SpriteEffects.None, 0.0f);

spriteBatch.End();
thease instruction to Draw function,the 3D primitive can not be displayed why?have you can fix the problem?

PARTNERS