I am making a game engine in Java. I am trying to add box collision to my game and this box needs to have the ability to rotate with the player. I have searched and found a formula which is supposed to calculate the new point of a rotated point, however, when I rotate my points they seem to follow a weird out of proportion figure 8 path instead of a circle around the center of my box.
for (Point p : points) {
//Loops through every point on the box (Square)
//top, left, bottom, right
float pointX = p.getX();
float pointY = p.getY();
//rotation as radians
float cos = (float) Math.cos(rotation);
float sin = (float) Math.sin(rotation);
pointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
pointY = centerY -(pointY-centerY) * cos + (pointX-centerX) * sin;
p.setPos(pointX, pointY);
}
Here is what happens to the box as I rotate my player:
https://gyazo.com/ff801ce8458269c2385e24b2dc5404f5
Any help would be greatly appreciated, I have been tackling this for almost a week now with the same results.
The problem is that you calculate pointY with the new value of pointX.
Thanks to #Imus answer for the proper calculation.
Try:
float pointX = p.getX();
float pointY = p.getY();
//rotation as radians
float cos = (float) Math.cos(rotation);
float sin = (float) Math.sin(rotation);
float newPointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
float newPointY = centerY +(pointY-centerY) * cos - (pointX-centerX) * sin;
p.setPos(newPointX, newPointY);
Remember that in java the Y axis is pointing downwards.
Not tested myself but you could try the following lines:
pointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
pointY = centerY +(pointY-centerY) * cos - (pointX-centerX) * sin;
I'm trying to add some distance (e.g. 10px) between a segment (arc) of the pie chart and it's center without success, here's what i've tried so far:
int value = 20; // example
double arcAngle = (value * 360 / 100);
double angle = 360 - (arcAngle / 2); // direction to add the distance to (center of arc)
double newX = pieCenterX + Math.cos(angle * Math.PI / 180.0) * 10;
double newY = pieCenterY + Math.sin(angle * Math.PI / 180.0) * 10;
// then drawing the arc with new x and y
g.fill(new Arc2D.Double(newX, newY, bounds.getWidth(), bounds.getHeight(), startAngle, arcAngle, Arc2D.PIE));
Ideally i should end up with something like that:
I don't know much on how to approach this, so my code was taken from examples i found elsewhere.
Usually zero angle is OX direction (right). So you have to make correction by 90 degrees (if your coordinate system is counterclockwise)
double angle = 90 + 360 - (arcAngle / 2);
My pitches and yaws are messed up.
I have the pitch and yaw of the pad but the beam's pitch and yaw are messed up.
How do i calculate the normal vector for the pad's pitch and yaw? I tried a crap load of math off of stackoverflow but they all failed so far.
What I first tried was adding 90 to the pitch of the pad but the yaw stayed messed up:
And this is what happens when I used to the pad's pitch and yaw as is to calculate the direction vector:
What I tried doing next was split the beam's pitch and yaw from the pad's pitch and yaw and have them both separately calculated. That mostly worked but the yaw was still completely messed up.
What I used to calculate the direction vector from yaw and pitch of the beams was a util minecraft uses to do so for mobs:
public static Vec3d getVectorForRotation3d(float pitch, float yaw) {
float f = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
float f1 = MathHelper.sin(-yaw * 0.017453292F - (float) Math.PI);
float f2 = -MathHelper.cos(-pitch * 0.017453292F);
float f3 = MathHelper.sin(-pitch * 0.017453292F);
return new Vec3d((double) (f1 * f2), (double) f3, (double) (f * f2));
}
But that failed obviously, so lastly, i tried the following using the pad's pitch:
double pitch = ((te.getPadPitch() + 90) * Math.PI) / 180;
double yaw = ((te.getPadYaw() + 90) * Math.PI) / 180;
double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vec3d lookvec = new Vec3d(x, y, z);
And this worked perfectly for the yaw but failed for the pitch
the pitch and yaw are both calculated in the way the player head rotates.
The pad's pitch and yaw are 100% correct when I use them on the pad itself but mess up when I use them on the beam. These are both using GL functions
Although the pitch and yaw don't respect the player's head's orientation system, it works with the pad.
For example, this is the yaw of the mirror in this pic and it's perfect for it's current value
And the pad is rotated like this:
GlStateManager.rotate(te.getPadYaw(), 0, 0, 1);
GlStateManager.rotate(te.getPadPitch(), 1, 0, 0);
And the line is drawn as such:
public static void drawConnection(BlockPos pos1, BlockPos pos2, Color color) {
GlStateManager.pushMatrix();
GL11.glLineWidth(1);
GlStateManager.disableTexture2D();
GlStateManager.color(color.getRed(), color.getGreen(), color.getBlue(), 0.7f);
GlStateManager.translate(0.5, 0.7, 0.5);
VertexBuffer vb = Tessellator.getInstance().getBuffer();
vb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION);
vb.pos(pos2.getX() - pos1.getX(), pos2.getY() - pos1.getY(), pos2.getZ() - pos1.getZ()).endVertex();
vb.pos(0, 0, 0).endVertex();
Tessellator.getInstance().draw();
GlStateManager.enableTexture2D();
GlStateManager.popMatrix();
}
I'm getting the pos1 and pos2 like so [CURRENTLY, MOST RECENTLY]:
double pitch = ((te.getPadPitch() + 90) * Math.PI) / 180;
double yaw = ((te.getPadYaw() + 90) * Math.PI) / 180;
double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vec3d lookvec = new Vec3d(x, y, z);
Vec3d centervec = new Vec3d(te.getPos().getX() + 0.5, te.getPos().getY() + 0.8, te.getPos().getZ() + 0.5);
Vec3d startvec = centervec.add(lookvec);
Vec3d end = startvec.add(new Vec3d(lookvec.xCoord * 30, lookvec.yCoord * 30, lookvec.zCoord * 30));
RayTraceResult result = te.getWorld().rayTraceBlocks(startvec, end, true, false, true);
Utils.drawConnection(te.getPos(), result.getBlockPos(), Color.RED);
How do i calculate the normal vector or a vector that's perpendicular to the pad properly from the pitch and yaw of the pad?
I'm at a loss at this point because I tried nearly everything I found on google for the most part with no luck.
EDIT: I've been told that splitting the beam pitch and yaw from the pad's pitch and yaw shouldn't be necessary, and I agreed but I just can't get it to work otherwise. Why is the beam drawing math different from the pad math?
Im not sure if this is your problem but for me mc returned some messed up yaw positions when i was dealing with them this is how i got that fixed
if (Yaw < -180.0) Yaw += 360;
else if (Yaw > 180) Yaw -= 360;
Minecraft yaw rotations are a bit wierd. They are not clamped between 0 and 360. For example, if you rotate your head to the right for a very long time, you can end up with rotations that are over a 1000 degrees. When clamped between 0 and 360 degrees, 1000 degrees becomes 280 degrees. If the number is negative, then you should subtract the number from 360. This is an example on clamping an angle between 0 and 360:
public static float clamp(float angle) {
angle = angle % 360; // Gets the remainder when dividing angle by 360
if(angle < 0) {
angle = 360 - angle;
}
return angle;
}
If you set your angles with this, things will always be between 0 and 360. Also, this is a snippet directly from MCP source code where rotation yaw/pitch was translated into a rotation vector:
/**
* Creates a Vec3 using the pitch and yaw of the entities rotation.
*/
protected final Vec3 getVectorForRotation(float pitch, float yaw)
{
float f = MathHelper.cos(-yaw * 0.017453292F - (float)Math.PI);
float f1 = MathHelper.sin(-yaw * 0.017453292F - (float)Math.PI);
float f2 = -MathHelper.cos(-pitch * 0.017453292F);
float f3 = MathHelper.sin(-pitch * 0.017453292F);
return new Vec3(f1 * f2, f3, f * f2);
}
I have a vector called, say, ship which every frame I need to set to a certain rotation around another vector called center. Here's a picture to show what I mean.
As you can see, I'd like to be able to rotate ship to any rotation around center, preferably in a function with 2 Vector2 parameters for the positions and then a float for the rotation (in degrees).
The mathematical functions to rotate a point around another point are:
double new_x = current_x * Math.cos(angle) - current_y * Math.sin(angle);
double new_y = current_x * Math.sin(angle) + current_y * Math.cos(angle);
if your center isn't at (0,0), you should first substract it at the current values, then add them again at the end like so:
double new_x = (current_x-center_x) * Math.cos(angle) - (current_y-center_y) * Math.sin(angle) + center_x;
double new_y = (current_x-center_x) * Math.sin(angle) + (current_y-center_y) * Math.cos(angle) + center_y;
Beware, your angle should be in radians and not in degrees, a easy conversion happens like so:
double angle = Math.toRadians(degrees);
Hope this helped!
So I have a Java2D game when I have to rotate a cannon to the mouse's direction and save the angle. It works but I have the real issue. I can't calculate correct X and Y velocity from this answer i got from this equation:
double angle = Math.atan2(centerY - c.mouseY, centerX - c.mouseX) - Math.PI / 2;
then i use this equation:
int x = (int) Math.acos(c.rotation*Math.PI/180);
int y = (int) Math.sin(c.rotation*Math.PI/180);
I don't get the right velocity for y. can you guys help me?
Maybe because your using cosine when calculating y value. Shouldn't it be sine?