5 Replies - 1570 Views - Last Post: 09 October 2012 - 03:20 AM

#1 @-Codeing-4-Fun-@  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 12

[XNA] Align object to another objekt?

Posted 07 October 2012 - 05:06 AM

Hello, my question is:
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);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: [XNA] Align object to another objekt?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: [XNA] Align object to another objekt?

Posted 07 October 2012 - 11:37 AM

Moved to XNA
Was This Post Helpful? 0
  • +
  • -

#3 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 536
  • View blog
  • Posts: 1,193
  • Joined: 24-April 12

Re: [XNA] Align object to another objekt?

Posted 08 October 2012 - 06:18 AM

What do you mean that you are trying to "align an object to another object"? Do you mean you want one object to point to another? Do you mean that you want to place them at the same spot and facing the same direction? Do you mean you want them to be parallel with one another at a perpendicular angle some distance away (which is what align means in my mind)?
Was This Post Helpful? 0
  • +
  • -

#4 @-Codeing-4-Fun-@  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 12

Re: [XNA] Align object to another objekt?

Posted 08 October 2012 - 11:24 AM

i mean this: i search the rotation (i hope the graphic will help)
Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 536
  • View blog
  • Posts: 1,193
  • Joined: 24-April 12

Re: [XNA] Align object to another objekt?

Posted 08 October 2012 - 02:55 PM

So, I still don't understand what your question is. If you're trying to find the angle from the ship to the mouse point then this would likely work:

Vector2 Ship;
Vector2 Mouse;
float AnswerInRadians;

AnswerInRadians = FindAngleOfPoint(Ship, Mouse);

public float FindAngleOfPoint(Vector2 ObjectPosition, Vector2 PointPosition)
{
  Vector2 VectorBetweenObjects;
  float AngleOfPoint;


  VectorBetweenObjects = PointPosition - ObjectPosition;
  AngleOfPoint = Math.Atan2((double)VectorBetweenObjects.x, (double)VectorBetweenObjects.y);
  
  return AngleOfPoint;
}




I think the code below is "essentially" the exact same thing, although it doesn't really use vectors. Also, I'm not sure whether it's ship - mouse or mouse - ship. If I got it wrong, it will point 180 degrees in the opposite direction. Also, the answer is in radians; so you may want to convert to degrees.
Vector2 Ship;
Vector2 Mouse;
float AnswerInRadians;

AnswerInRadians = FindAngleOfPoint(Ship, Mouse);

public float FindAngleOfPoint(Vector2 ObjectPosition, Vector2 PointPosition)
{
  return Math.Atan2((double)(PointPosition.x - ObjectPosition.x), (double)(PointPosition.y - ObjectPosition.y));
}



This post has been edited by BBeck: 08 October 2012 - 02:57 PM

Was This Post Helpful? 1
  • +
  • -

#6 @-Codeing-4-Fun-@  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 12

Re: [XNA] Align object to another objekt?

Posted 09 October 2012 - 03:20 AM

thanks... this I have searched :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1