• Advertisement
Sign in to follow this  

C++ 2D Math Problem

Recommended Posts

In the following code:


 

Point p = a[1];  center of rotation

for (int i = 0; I<4; i++)
{
 	int x = a.x - p.x;
 	int y = a.y - p.y;
 	
   	a.x = y + p.x;
   	a.y = - x + p.y;
}

 

I am understanding that a 90 degree shift results in a change like:   

xNew = -y

yNew = x

 

Could someone please explain how the two additions and subtractions of the p.x and p.y works?

 

Thank you,

Josheir

Edited by Josheir

Share this post


Link to post
Share on other sites
Advertisement
xNew = -y
yNew = x

This rotates a point/vector around the origin by 90 degrees

The original code snippet does the same (similar -- they're mirrored, so if using the same coordinate system it would be the reverse direction), except it rotates about a point other than the origin, which in this case is the center of the rectangle. It's effectively the same as rotating around the origin, except it offsets by the center point's position in order to treat the center point as the origin.

 

EDIT: for clarity, the two subtractions move the rectangle points to a position relative to the world origin that is the same as their position relative to their center point. Then they are rotated around the origin, then moved back (the two additions) to be relative to the center point instead of the world origin.

Edited by cmac

Share this post


Link to post
Share on other sites

So, is this correct:  The first two p's subtract the offset to regard the point not being the origin.  Than the computation is done again and p's are added to re translate it back to the origin.

 

The array is four integers held to be a shape of a grid of 8 cells (two columns,)  It's not really a rectangle.

 

I was playing around with the values (mirrored or not:)

xNew = -y

yNew = x

and got it working luckily.  I find it confusing that the second set of additions uses the first set that subtracts.  I think I've got it now but was hoping someone could do a simple explanation if there is anything else to explain!

 

The way that I got it working was I drew a circle with p.y in the center and a.y on the circumference with the difference being what I assumed to be  the y.  Likewise for the x values.

 

Thank you,

Josheir

 

 

 

Edited by Josheir

Share this post


Link to post
Share on other sites

If you know how to rotate around the origin, you can rotate around any point P like this:

  • translate the whole picture so P becomes the origin,
  • rotate around the origin,
  • translate back so the origin becomes P

 

Does that help?

 

Share this post


Link to post
Share on other sites

Yes, all that helps; I'm pretty sure I understand it now.  I am a bit confused by the diagram though.  I'll go over my program again tomorrow but I do understand what you mean.  I need to translate this understandable concept into the understandability of the idea for a specific program.  Like I said, it's not really a rectangle, it's more complicated.

Thanks again,

Josheir

Share this post


Link to post
Share on other sites

Black dot = polygon center, red line = subtraction, green line = rotation, blue line = addition. As you can see, the point ends up at the desired point (90 degrees rotated around center).

Overall Alvaro's explanation was much more elegant and concise than mine, so just focus on that.

Share this post


Link to post
Share on other sites

The subtraction (red line) translates the picture so that the polygon center has become (0,0).

Edited by cmac

Share this post


Link to post
Share on other sites

So in the image, there is P and center. P is a distance from center. The red line is P - C, this puts P a distance from origin. Green line is a rotation around origin = Pr. Blue line is Pr + C, moves the rotated point back out to be around center, and is the final rotated point.  

Share this post


Link to post
Share on other sites

This results from the dot product. Say you have a vector u and you are looking for a vector v orthogonal to u. You know that the dot product between two orthogonal vectors must be zero. Hence

u.x * v.x + u.y * v.y = 0

Common sense shows that the simplest solution here is to choose:

v.x = u.y and v.y = u.x and negate either v.x or v.y

E.g choose:

v.x = u.y and  v.y = -u.x  

And you get

 u.x * u.y - u.y * u.x = 0

The same is true for v.x = -u.y and  v.y = u.x

Geometrically choosing v.x to be negative is equivalent to a 90 degree CCW rotation around the origin while choosing v.y to be negative represents a 90 degree CW rotation around the origin. This is easy to verify.

You will find the same idea applied in 3D as well. Two coordinates are flipped like in 2D and the third coordinate is chosen to zero. Erin posted an elegant solution in 3D on his blog some years ago:

http://box2d.org/2014/02/computing-a-basis/

HTH,

-Dirk

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By mister345
      Hi guys, so I have about 200 files isolated in their own folder [physics code] in my Visual Studio project that I never touch. They might as well be a separate library, I just keep em as source files in case I need to look at them or step through them, but I will never actually edit them, so there's no need to ever build them.
      However, when I need to rebuild the entire solution because I changed the other files, all of these 200 files get rebuilt too and it takes a really long time.
      If I click on their properties -> exclude from build, then rebuild, it's no good because then all the previous built objects get deleted automatically, so the build will fail.
      So how do I make the built versions of the 200+ files in the physics directory stay where they are so I never have to rebuild them, but
      do a normal rebuild for everything else? Any easy answers to this? The simpler the better, as I am a noob at Visual Studio settings. Thanks.
    • By Snaked
      Im working in this project for 1 year .... mostly i develop a tool and databases for make the different maps and now i'm doing the client for play the game
      Tell me if you like it......
      this is a capture of how is viewing atm

       
       
      https://youtu.be/9251v4wDTQ0
    • By reenigne
      For those that don't know me. I am the individual who's two videos are listed here under setup for https://wiki.libsdl.org/Tutorials
      I also run grhmedia.com where I host the projects and code for the tutorials I have online.
      Recently, I received a notice from youtube they will be implementing their new policy in protecting video content as of which I won't be monetized till I meat there required number of viewers and views each month.

      Frankly, I'm pretty sick of youtube. I put up a video and someone else learns from it and puts up another video and because of the way youtube does their placement they end up with more views.
      Even guys that clearly post false information such as one individual who said GLEW 2.0 was broken because he didn't know how to compile it. He in short didn't know how to modify the script he used because he didn't understand make files and how the requirements of the compiler and library changes needed some different flags.

      At the end of the month when they implement this I will take down the content and host on my own server purely and it will be a paid system and or patreon. 

      I get my videos may be a bit dry, I generally figure people are there to learn how to do something and I rather not waste their time. 
      I used to also help people for free even those coming from the other videos. That won't be the case any more. I used to just take anyone emails and work with them my email is posted on the site.

      I don't expect to get the required number of subscribers in that time or increased views. Even if I did well it wouldn't take care of each reoccurring month.
      I figure this is simpler and I don't plan on putting some sort of exorbitant fee for a monthly subscription or the like.
      I was thinking on the lines of a few dollars 1,2, and 3 and the larger subscription gets you assistance with the content in the tutorials if needed that month.
      Maybe another fee if it is related but not directly in the content. 
      The fees would serve to cut down on the number of people who ask for help and maybe encourage some of the people to actually pay attention to what is said rather than do their own thing. That actually turns out to be 90% of the issues. I spent 6 hours helping one individual last week I must have asked him 20 times did you do exactly like I said in the video even pointed directly to the section. When he finally sent me a copy of the what he entered I knew then and there he had not. I circled it and I pointed out that wasn't what I said to do in the video. I didn't tell him what was wrong and how I knew that way he would go back and actually follow what it said to do. He then reported it worked. Yea, no kidding following directions works. But hey isn't alone and well its part of the learning process.

      So the point of this isn't to be a gripe session. I'm just looking for a bit of feed back. Do you think the fees are unreasonable?
      Should I keep the youtube channel and do just the fees with patreon or do you think locking the content to my site and require a subscription is an idea.

      I'm just looking at the fact it is unrealistic to think youtube/google will actually get stuff right or that youtube viewers will actually bother to start looking for more accurate videos. 
    • By mister345
      Hi, can someone please explain why this is giving an assertion EyePosition!=0 exception?
       
      _lightBufferVS->viewMatrix = DirectX::XMMatrixLookAtLH(XMLoadFloat3(&_lightBufferVS->position), XMLoadFloat3(&_lookAt), XMLoadFloat3(&up));
      It looks like DirectX doesnt want the 2nd parameter to be a zero vector in the assertion, but I passed in a zero vector with this exact same code in another program and it ran just fine. (Here is the version of the code that worked - note XMLoadFloat3(&m_lookAt) parameter value is (0,0,0) at runtime - I debugged it - but it throws no exceptions.
          m_viewMatrix = DirectX::XMMatrixLookAtLH(XMLoadFloat3(&m_position), XMLoadFloat3(&m_lookAt), XMLoadFloat3(&up)); Here is the repo for the broken code (See LightClass) https://github.com/mister51213/DirectX11Engine/blob/master/DirectX11Engine/LightClass.cpp
      and here is the repo with the alternative version of the code that is working with a value of (0,0,0) for the second parameter.
      https://github.com/mister51213/DX11Port_SoftShadows/blob/master/Engine/lightclass.cpp
    • By Rannion
      Hi, I am sending data to peers and those data need to be retreived from a scenegraph with a mutex to lock the data.
      The process of gathering the data is taking a bit less than a ms. I'm starting the thread every time I want to gather the data. If I'm running at 60 fps, I'm starting the thread 60 times per second so is that a performance or design problem?
      Would it be much better to have the thread always running and some kind of mechanism to ask him to perform the task whenever it's needed, so around 60 or 120fps?
      Also, does starting a thread creates some memory alloc/dealloc and then produce on the long run some kind of fragmentation?
      Thank you all.
  • Advertisement