Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 May 2013
Offline Last Active Mar 06 2014 01:58 AM

#5122829 Rotate sprites around a point in 2D

Posted by on 11 January 2014 - 06:55 AM

Glad to hear it! :)

Cudos on coming back with your solution for future reference :)



#5122116 Rotate sprites around a point in 2D

Posted by on 08 January 2014 - 03:16 AM

Hi Dobbydoo smile.png

It sounds like there are two different tasks here:


1) Getting the mouse's position relative to the parent object (where you want the childobject attached, right?).

2) Drawing the child object in-game while being able to rotate its position and Texture.



Solving 1 is pretty simple:


To get direction from object to mouse as a Vector2, subtract object's position (a Vector2) from mouse's position (also a Vector2):


Vector2 distanceAndDirectionFromObjectToMouse = mousePosition - objectPosition;


This value you can store along with the child object, to know where to draw it in relation to the parent.


Solving 2) is more complicated.

You want to be able to rotate the child object's position around the parent.

To do this you need to:

  1. Find the current rotation of and distance to the child object
  2. change the rotation by the wanted amount
  3. calculate the new position using cos and sin (you may want to read up on these)
    basically cos(angle) gives you the X coordinate of where to position something, and sin(angle) gives you the Y coordinate.

Getting the rotation of a Vector2 is possible using the Atan2 function:


private float Vector2ToRadian(Vector2 direction)
    return (float)Math.Atan2(direction.X, -direction.Y);


Radian is  a type of measurement where there are 2 * PI degrees (approximately 6.283) around the full circumference of the circle as opposed to regular degrees where you have 360.


Getting the distance is a method on the Vector2 struct called Length().


float distanceToChild = distanceAndDirectionFromObjectToMouse.Length();


using the distance and rotation you can use the following method to find the new location of the child object (the "sattelite" in the methodname below):


public Vector2 GetPositionOfSatellite(Vector2 center, float distance, float directionInRadians)  


          float yDifference = (float)Math.Sin(directionInRadians);

          float xDifference = (float)Math.Cos(directionInRadians);

          Vector2 direction = new Vector2(xDifference, yDifference);

          Vector2 precisePositionOfSatellite = center + direction * distance;

          return precisePositionOfSatellite;      


If that is unclear, you may benefit from the two articles I've taken code from:





...otherwise, just ask smile.png


Kind regards - Jakob

#5091094 XNA - Tile Engine With Collision

Posted by on 02 September 2013 - 01:11 PM

Hi :)


Here you can download codesamples for reading a map from a textfile.



This code uses numeric values, but you can change what you do with the characters read to a SWITCH statement like you are mentioning in your question.


Kind regards - Jakob

#5081773 Loading sprites (big map) XNA

Posted by on 30 July 2013 - 02:01 PM

Here's a code framework for infinite worlds implemented in XNA: http://xboxforums.create.msdn.com/forums/p/111091/664227.aspx#664227


You would have to make an implementation of IPermanentMapStorage which gets maps from a webservice. 

You can probably use the rest of the code as is smile.png




P.S. in case you are interested in seeing the newest version, which uses Perlin noise for map generation (not a requirement for the framework), it is attached to this post.

Attached Files

#5060552 Help with Worm Clone Draw() method

Posted by on 09 May 2013 - 06:39 AM

Hi Manhattanisgr8 :) 


I am currently making a tutorial on how to code a snake game in XNA, so you're welcome to have my code so far to look at for inspiration.

I've chosen the List<Point> implementation, and then just drawing the List[0] part as a different texture to show the head.


Here's the code, and here's a screendump of the game so far:




Let me know if any of it gives you any trouble :)


Kind regards - Jakob