How can I align a object to another objekt (A ship to the mouse)?
I have tryed Math.Atan2(X,Y) but this is not what I have searched...
Here the Code of Drawing and another what is important (I hope it is not too confusing) :
It's a 2D game.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Windows.Forms;
namespace SpaceGame
{
class Camera // Camera class
{
float spd;
Game1 game;
public Matrix transform;
Viewport view;
Vector2 centre;
Texture2D spriteTexture;
Vector2 spriteOrigin = Vector2.Zero;
public float rotation;
Vector2 spriteVelocity;
const float tangentialVelocity = 5f;
float friction = 0.1f;
public Rectangle spriteRactangle = new Rectangle();
public Vector2 spritePosition;
Rectangle spriteBatchRectangle;
Vector2 spriteOrgin;
Convert convert;
Debuging debug;
public Camera(Viewport newView, Game1 game, Convert convert, Debuging debuging)
{
this.convert = convert;
debug = debuging;
this.game = game;
view = newView;
}
public void Load(ContentManager Content)
{
if (game.ship == "phoenix")
{
// Speed:
spd = 3.20f;
}
spriteTexture = Content.Load<Texture2D>(game.ship);
spritePosition = new Vector2(convert.ToInt(game.XPOS, 0), convert.ToInt(game.YPOS, 0));
}
public void Update(GameTime gametime, Game1 ship)
{
Vector2 shipPosOld = spritePosition;
MouseState mState = Mouse.GetState();
spriteBatchRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y, spriteTexture.Width, spriteTexture.Height);
spritePosition = spriteVelocity + spritePosition;
spriteOrgin = new Vector2(spriteRactangle.Width / 2, spriteRactangle.Height / 2);
double mKordX = mState.X;
double mKordY = mState.Y;
float winkel = (float)(Math.Atan2(mKordY, mKordX) * 40);
if (mState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
{
spriteVelocity.X = ((float)Math.Cos(rotation) * tangentialVelocity) * spd;
spriteVelocity.Y = ((float)Math.Sin(rotation) * tangentialVelocity) * spd;
}
else if (spriteVelocity != Vector2.Zero)
{
if (mState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
{
float i = spriteVelocity.X;
float j = spriteVelocity.Y;
spriteVelocity.X = (i -= friction * i) * spd;
spriteVelocity.Y = (j -= friction * j) * spd;
}
else if (mState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
{
spritePosition = shipPosOld;
}
}
if (spritePosition.X < 0)
{
spritePosition.X = 0; // You cant fly away
}
if (spritePosition.Y < 0)
{
spritePosition.Y = 0; // You cant fly away
}
if (spritePosition.X > 4096)
{
spritePosition.X = 4096; // You cant fly away
}
if (spritePosition.Y > 3072)
{
spritePosition.Y = 3072; // You cant fly away
}
rotation = winkel;/*(MathHelper.ToDegrees((float)winkel));*/
/////////////////////////////// UPDATE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
centre = new Vector2(spritePosition.X + (spriteRactangle.Width / 2) - 943, spritePosition.Y + (spriteRactangle.Height / 2) - 512); // Center of Screen
transform = Matrix.CreateTranslation(new Vector3(1, 1, 0)) * Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0)); // Camera
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ UPDATE END //////////////////////////////
debug.Update(centre.X, centre.Y, winkel, rotation, spritePosition.X, spritePosition.Y);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(spriteTexture, spritePosition, null, Color.White, rotation, spriteOrgin, 1f, SpriteEffects.None, 0); // Draw the ship
}
private void ResetMouse() // Test Function... I'm not using this function
{
Mouse.SetPosition(943,512);
}
}
}

New Topic/Question
Reply



MultiQuote





|