6 Replies - 1026 Views - Last Post: 12 July 2012 - 05:41 AM

#1 KadajettGaming  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 78
  • Joined: 27-June 12

How do I keep a fixed point during rotation?

Posted 07 July 2012 - 04:22 PM

Sorry if the topic was worded weirdly, best I could do. Regardless, I am trying to have bullets shoot out at the tip of a gun. Problem is, the tip rotates with the player. How do I calculate the position of the tip of the gun?

Here is a little idea of what I am having a problem with.

The red star is where the bullets will come out(obviously lol)

And the blue star is the pivot point.

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: How do I keep a fixed point during rotation?

#2 BBeck  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 562
  • Joined: 24-April 12

Re: How do I keep a fixed point during rotation?

Posted 07 July 2012 - 09:49 PM

That's quite a nice little Trig problem you've got there. It's actually a little more complicated than the question that you "asked"; you've also got the problem of which way the gun barrel is facing. I assume you don't want the gun to just shoot down the Y axis no matter which way it's facing.

Maybe you're planning on axis aligning the gun so that it only faces down the X or Y axis, but you could have it face any direction if you want.

Anyway, you could "probably" solve this as a standard Trig problem, but it's a really probably best handled as a vector problem. I think any professional game designer would handle it as a vector problem.

So, I would solve it with three vectors. The first vector is the position of the blue dot. It's amount/length would be the length of the line between the origin (0,0) and the blue dot. The head of the vector would store the position of the blue dot.

Then I would have two additional vectors. One for forward, and one for perpendicular of forward. The forward vector would have it's tail at the origin (0,0) (just like all vectors in XNA) and the perpendicular vector would also have it's tail at the origin. The forward vector would start out as (0, y position of the red dot) so that it not only has the y distance from the blue dot to the red dot, but also points in the forward direction. The perpendicular vector would be at a 90 degree angle and would start out as (x position of the red dot, 0). Adding the forward vector to the perpendicular vector will give you a vector who's length/magnitude is the distance from the blue dot to the red dot.

The blue dot has to be at the origin in order to rotate the model. But representing it this way allows you to kind of ignore that because the forward vector and the perpendicular will never move from having their tails at the origin, and therefore you can rotate them successfully without changing the position vector because it's stored seperately.

So, once you've got your three vectors you're ready to go. The position of the blue dot will just be whereever the head of the position vector is at. The position of the red dot will be at the position of the result of adding all three vectors together.

To rotate the model, you will rotate the forward vector and the perpendicular vector by the same amount (so that the perpendicular stays perpendicular). After you rotate those two vectors, you can still add all three together to get the position of the red dot.

But that still leaves the problem of what direction the barrel of the gun is facing. But it will be facing in the same direction as the rotated "forward" vector. So whatever direction that vector is facing is the direction that the bullets should travel.

A big part of understanding the problem is understanding that the line between the blue dot and the red dot is the hypotenuse of a right triangle. It forms a rectangle with two right triangles, one on either side of the hypotenuse. The forward vector is the side adjacent for one triangle and the perpendicular vector is the side adjacent for the other right triangle. They also are the length of the side opposite for each others' triangles.

I would also point you to my tutorial on vectors, at my website, if you aren't familiar with vectors.

http://xna-3d-101.co...ls/Vectors.html

To recap, the positional vector stores the displacement of the blue dot from the origin. The forward and perpendicular vectors added together give you the displacement of the red dot from the blue dot. Add the positional vector to them and you will get the position of the red dot. Plus, the forward vector will tell you what direction the barrel is facing.

This post has been edited by BBeck: 07 July 2012 - 10:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: How do I keep a fixed point during rotation?

Posted 08 July 2012 - 07:54 AM

when drawing the sprite you can use the following method


s.Draw(texture, position, null, Color.White, ROTATION_IN_RADIANS, ORIGIN_POINT, Microsoft.Xna.Framework.Graphics.SpriteEffects.None, 0);



With the rotation, I hope that you already have that worked out? Otherwise there are some pretty simple methods of working out rotation in radians. I would suggest keeping this as a variable in your player class or w/e it is you need to rotate. As for the origin point, that's your blue dot, you need to work out the X/Y position for that and store it in either your player class or if you use a sprite class store it in that.


Edit:
Sorry too many windows open, here is a valid answer to your question:

Rotating a Point around an Origin

You can use this method to save a point that your bullets will spawn, when rotating you use this method to calculate a new point the bullets will spawn from.

This post has been edited by racidon: 08 July 2012 - 07:58 AM

Was This Post Helpful? 1
  • +
  • -

#4 BBeck  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 562
  • Joined: 24-April 12

Re: How do I keep a fixed point during rotation?

Posted 08 July 2012 - 11:19 AM

Kadajett, I think you'll find what Racidon posted there very helpful. I would add that those rotation formulas are the necessary rotation formulas to rotate the heads of the forward vector and the perpendicular vector I talked about in my post.

Because of the way I defined the forward vector and the perpendicular vector in my example, they always have their tail at the origin, and so you don't have to do any special translation to the origin to make them work properly.

I may be over complicating things for you, if you keep everything axis aligned, or at some combination of 90 or 45 degrees. Really, what I'm telling you with vectors "frees" you from those alignments and allows the gun to face any possible direction. If you keep the whole thing aligned to some combination of 45 degree turns than you can keep track of which way the barrel is facing just by keeping track of the number of 45 degree turns made.

Racidon showed you a way to rotate the sprite around an arbitrary point. So, it may not be necessary to go into the complexity of handeling it with vectors if you keep things aligned to some combination of 45 degrees and track the facing of the sprite by the number of 45 degree rotations it has gone through (reseting every 360 degrees).

With 45 degree rotations you run into the problem of the sprite no longer being axis aligned (unlike with 90 degree rotations). But you could use the rotation formulas to calculate the X and Y position of the gun barrel relative to it's offset from the blue dot. In other words, you could define the X,Y position of the red dot minus the X,Y position of the blue dot to get the offset between them. Then you could use the rotation formulas to get the new offset position of the red dot after a 45 degree rotation. That's going to be a fixed X,Y location for 45 degrees because turning to 45 degrees will always give the same position.

Adding the rotated offset back to the blue dot position would give you the position of the red dot.

This post has been edited by BBeck: 08 July 2012 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 KadajettGaming  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 78
  • Joined: 27-June 12

Re: How do I keep a fixed point during rotation?

Posted 09 July 2012 - 06:03 AM

I just got home from work, when I wake up, I will take a look through all off these. Thanks a lot guys. Oh, and I already have rotation of the player. I just need to track the red dot. Have a nice one :sleep1:
Was This Post Helpful? 0
  • +
  • -

#6 KadajettGaming  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 78
  • Joined: 27-June 12

Re: How do I keep a fixed point during rotation?

Posted 10 July 2012 - 04:43 PM

Ok, finally had a chance to look through everything, and I am planning out my implementation. Amazing explanation you guys, it took me a min to understand it lol but now that I do, I feel like I can plan a bunch of new thing into the game that I wouldn't have been able to before. This is next on my priority list after figuring out per pixel collision with rotated sprites :P
Was This Post Helpful? 1
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: How do I keep a fixed point during rotation?

Posted 12 July 2012 - 05:41 AM

There are some good examples of collision detection on app hub. They start with basic bounding box collision detection and go from there all the way up to rotated and scaled sprites per pixel. You shouldn't have to do per pixel collision detection though. That ends up being very expensive. Using multiple bounding boxes is usually the better approach. You can do collision detection between rotated bounding boxes easily enough. Again, there are some good examples of that on app hub. The third one is what you're looking for but the other two are useful for others reading the thread.

http://create.msdn.c...on_2d_rectangle
http://create.msdn.c...ion_2d_perpixel
http://create.msdn.c...xel_transformed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1