简体   繁体   中英

How to update x, y coordinates on an rotating rectangle edge

Ok so I have searched and searched for a solution to my problem, but non seem to fix it.

I need to make a game with a rotating "cannon", my cannon is a simple rectangle placed in the middle of my panel that I can rotate with my keyboard. It rotates around one edge. I want to shoot out of the edge on the other side. I have found the starting point of where to shoot my bullets by using:

x = a + dia * (float)Math.Cos(angle);
y = b + dia * (float)Math.Sin(angle)

where "a, b" is the center coordinate I rotate it around and "dia" is the diagonal of the rectangle and "angle" is the angle of the one half of my rectangle.

public float rotate = 0.0f; 
g.TranslateTransform(a , b);
        g.RotateTransform(rotate);

I have a own class for my bullets that I put in a List. So far so good. But when I rotate my cannon, the bullets don't come out from the tip anymore..they just start appearing far off where I want them to. it's because of this code:

x = (float)((x * Math.Cos(rotate)) - (y * Math.Sin(rotate)));
        y = (float)((x * Math.Sin(rotate)) + (y * Math.Cos(rotate)));

that's supposed to update the x, y coordinates of the tip of the cannon. If I delete it, it just fires from the same spot(no shit). Can someone please explain to me what code I need to write to update the X, Y so they come out of my rectangle edge? It's driving me crazy..

Edit: Found my answer staring at the screen in the early mornings. I had no need for any "find new x, y coordinates". I simply made a updater that updated the original angle with the float number it needed to move a little bit each time i rotated it. hah! so simple, yet so hard to see.

First of all,

x = (float)((x * Math.Cos(rotate)) - (y * Math.Sin(rotate)));
y = (float)((x * Math.Sin(rotate)) + (y * Math.Cos(rotate)));

needs to be something like:

float oldx = x;
float oldy = y; 
x = (float)((oldx * Math.Cos(rotate)) - (oldy * Math.Sin(rotate)));
y = (float)((oldx * Math.Sin(rotate)) + (oldy * Math.Cos(rotate)));

your new values need to be based purely off the old values..

If there's any other problem after fixing this, it may be related to how the rectangle is translated on the plane.

Edit: If this were a code review, I'd say the solution I just gave isn't quite the best solution either (it just doesn't suffer from the bug you introduced by using the new value of x to calculate the new value of y). See, Math.Cos and Math.Sin are generally expensive operations compared to multiplication and addition. If you had a bunch of points that need transformed the same way, best to calculate Math.Sin(rotate) and Math.Cos(rotate) once and use those values for every point. This might be a good place to use the Flyweight pattern and define a class where an instance would hold all your points for a given object/context so that operations can be done in aggregate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM