Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


issue with image placement in respect of another image


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 kingpinzs   Members   -  Reputation: 109

Like
0Likes
Like

Posted 10 May 2012 - 02:05 PM

I am using XNA 4.0 and c#

how can I get an image to set its position according to the end of another image?

this is what I have so far


                rocketPosition = players.Position;
                rocketPosition.X += players.Position.X +40;
                rocketPosition.Y -= players.Position.Y -370 ;
                rocketAngle = players.Angle;
                Vector2 up = new Vector2(0, -1);
                Matrix rotMatrix = Matrix.CreateRotationZ(rocketAngle);
                rocketDirection = Vector2.Transform(up, rotMatrix);
                rocketDirection *= players.Power / 50.0f;

the rocket goes in the right direction but its starting position is wrong if I move the cannon

the code that moves the cannon

if (keybState.IsKeyDown(Keys.Left))
            {
                if(players.Angle > MathHelper.PiOver4-.3)
                players.Angle -= 0.01f;
            }
            if (keybState.IsKeyDown(Keys.Right))
            {
                if (players.Angle < MathHelper.PiOver2)
                players.Angle += 0.01f;
            }


Sponsor:

#2 Jutaris   Members   -  Reputation: 478

Like
0Likes
Like

Posted 18 May 2012 - 03:15 PM

I accomplished this using something like this:

public Vector2 WeaponFireLoc
	    {
		    get
		    {
                return Location + (WeaponDir * <X Offset at neutral angle, I used 5 but have small sprites>);
		    }
	    }
	    public Vector2 WeaponDir
	    {
		    get
		    {
			    Vector2 v = new Vector2(-1, 0);
			    v = Vector2.Transform(v, Matrix.CreateRotationZ(WeaponAngle));
			    if (v != Vector2.Zero)
			    {
				    v.Normalize();
			    }
			    return v;
		    }
	    }

The WeaponDir gets a normalized V2 (-1 -> 1) that points in the direction the weapon is aiming.
WeaponFireLoc returns a V2 that is N pixels down that direction from Location, where N is <X Offset at neutral angle, I used 5 but have small sprites>.

Hope that helps. I suspect it's not the only solution, and perhaps not the best, but it's worked for me. Posted Image

My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

Come join us for some friendly game dev discussions over in XNA Chat!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS