I'm trying to create a program that creates a custom pattern. I have it so that if sides = 3, it's a triangle 4 = rect and anything else above that has a formula so that, if you really wanted, you could have 25 sides. I'm using lines, rotation and rotation to plant, turn, draw repeat.
angleMeasure = (180 * (sides-2) ) /sides;
println(angleMeasure);
println(radians(angleMeasure));
//creating the 5+ shape
pushMatrix();
translate(width/2, height/2); //translating the whole shape/while loop
while(counter < sides){
line(0,0,170,0);
translate(170,0);//THIS translate is what makes the lines go in the direction they need too.
rotate(angleMeasure);
counter = counter + 1;
This works almost correctly. The last and first lines don't connect. Suggestions? Maybe it's a problem with the math, but println reveals a correct angle measure in degrees. Here's what it looks like: http://i.stack.imgur.com/TwYMj.png
EDIT: Changed the rotate from rotate(angleMeasure) to rotate(angleMeasure * -1). This rotated the whole shape and made it clear that the angle on the very first line is off. See:http://i.stack.imgur.com/Z1KmY.png
You actually need to turn by angle=360°/sides. And convert this angle into radians.
Thus for a pentagram you need angle=72°. The number you computed is 108, which interpreted as radians is 34 full turns plus an angle of about 67°. This falls 5° short of the correct angle, so that you obtain a somewhat correct picture with slightly too wide inner angles, resulting in the gap (and not a crossing as when the angle were larger than the correct angle).
Related
I'm working on feature of a graphic editor where I'm editing arcs, and QPainterPath::arcTo is not behaving as I expected when the shape is an ellipse; it works as expected when it's a circle.
The two images below show the results. In the first case, I've created a circle, which I then convert to an arc with an initial start angle of 45 and span angle of 270. The scene coordinate space is square. The diagonal lines are at 45 degrees. As expected, the circular arc's end points are exactly on the diagonal lines.
In the second case, I have an ellipse, which is converted to an arc in exactly the same way with 45 and 270 degree angles respectively. The end points of the arc no longer fall on the diagonal lines, which is not what I expect.
In both cases, the drawing code is:
painter.arcTo (rect, 45, 270);
Zero degrees is at the 3 o'clock position, and I had believed that the specified angle was measured between that and a line from the center point to the point on the arc edge. Clearly, something else is going on that I don't understand and doesn't appear to be documented in the QPainter::arcTo description.
This is an issue because I'm writing code to reshape the arc, and I need to be able to work backgrounds when all I have is the current mouse position and the center point of the encompassing rectangle. Right now, as I reshape the arc, the angle that I'm calculating is only accurate at 0, 90, 180, and 270. The closer I get to the intervening 45 degree angles, the further off my angle is.
I'm getting that angle by:
QLineF (rect.center(), mouse_pos).angle ();
Again, for circles, this works perfectly. For non-circular ellipses, it doesn't.
After writing this up, I found this beautiful illustration, which exactly demonstrates what I'm dealing with. Unfortunately, the Postscript solution isn't helpful for me. I need to know what to do to calculate the correct angles.
I've found my answer here. As I expected, my understanding of the angles was incorrect. To perform my mouse tracking to reshape the arc, I need to find the intersection of a line segment with an ellipse and work backward from the parametric ellipse equations to find the correct angle.
Thanks to #goug I was able to fix a similar issue with QPainterPath::arcTo.
In my case I need an elliptical arc that behaves like a normal arc. Where start and end angles are controlled by a user. The start angle doesn't require any corrections, but the end angle does.
Here is a code that shows how to bypass this issue.
qreal startAngle = 10;
qreal endAngle = 60;
qreal radius1 = 30; // X-axis
qreal radius2 = 60; // Y-axis
QPointF center;
QRectF boundingRect(center.x() - radius1, center.y() - radius2, radius1*2, radius2*2);
if (!qFuzzyIsNull(endAngle) &&
!VFuzzyComparePossibleNulls(endAngle, 90) &&
!VFuzzyComparePossibleNulls(endAngle, 180) &&
!VFuzzyComparePossibleNulls(endAngle, 270) &&
!VFuzzyComparePossibleNulls(endAngle, 360))
{
// Calculating correct end angle
qreal endAngleRad = qDegreesToRadians(endAngle);
endAngle = qRadiansToDegrees(qAtan2(radius1 * qSin(endAngleRad),
radius2 * qCos(endAngleRad)));
}
QLineF startLine(center.x(), center.y(), center.x() + radius1, center.y());
QLineF endLine = startLine;
startLine.setAngle(startAngle);
endLine.setAngle(endAngle);
qreal sweepAngle = startLine.angleTo(endLine);
QPainterPath myPath;
myPath.arcTo(boundingRect, startAngle, sweepLength);
So I have a bit of a math problem. Here are the pieces.
Input:
Rot = Rotation (degrees). This is the rotation of the "player". This is also the yaw.
Vel.X = This is the left/rightward movement that would be happening if it weren't rotated
Vel.Z = Same as last except its up/down movement
Output:
Result.X = This is the actual movement that should be happening along the x axis considering rotation
Result.Z = Same as last
Basically the scenario is that a player is standing on a platform with "Rot" rotation. When directional keys are pressed velocity is added accordingly to the "Vel" value. However if rotation isn't 0 this wont produce the right result because when the player rotates moving left becomes relative.
Could you please tell me a formula that would find the proper x and y movement that would result in the player moving around relative to its rotation?
This problem is probably the most basic rotation question in game programming.
Using your Vel.X and Vel.Z values, you have what you might think of as the vector you wish to rotate in the x/z plane (instead of x/y - but same idea). Whether velocity or position, the approach is the same. With a simple google search we find that for 2D vector rotation, the formula is:
Result.X = Vel.X * cos(Rot) - Vel.Z * sin(Rot);
Result.Z = Vel.X * sin(Rot) + Vel.Z * cos(Rot);
I am a student in video games, and we are working on a raytracer in C++. We are using our teachers' library.
We create procedural objects (in our case a sphere), the Camera sends a ray for each pixel of the screen and the ray send back information on what it hit.
Some of us decided to integrate Normal Maps. So, at first, we sent ray on the object, looked at the value of the Normal map texel where we hit the sphere, converted it in a vector, normalized it and sent it back in place of the normal of the object. The result was pretty good, but of course, it didn't take the orientation of the "face" (it's procedural, so there is no face, but it gives the idea) into account anymore, so the render was flat.
We still don't really know how to "blend" the normal of the texture (in tangent space) and the normal of the object together. Here is our code:
// TGfxVec3 is part of our teachers library, and is a 3d vector like this:
// TGfxVec3( 12.7f, -13.4f, 52.0f )
// The sphere being at the origin and of radius 1, and tHit.m_tPosition being the
// exact position at the surface of the sphere where the ray hit, the normal of this
// point is the position hit by the ray.
TGfxVec3 tNormal = tHit.m_tPosition;
TGfxVec3 tTangent = Vec3CrossProduct( tNormal , m_tAxisZ );
TGfxVec3 tBiNormal = Vec3CrossProduct( tNormal , tTangent );
TGfxVec3 tTextureNorm = 2*(TGfxVec3( pNorm[0], pNorm[1], pNorm[2] )/255)-TGfxVec3( -1.0f, -1.0f, -1.0f );
// pNorm[0], pNorm[1], pNorm[2] are respectively the channels Red, Green,
// and Blue of the Normal Map texture.
// We put them in a 3D vector, divid them by 255 so their value go from 0 to 1,
// multiply them by 2, and then substract a vector, so their rang goes from -1 to +1.
tHit.m_tNorm = TGfxVec3( tTangente.x*tTextNorm.x + tCoTangente.x*tTextNorm.x +
tNorm.x*tTextNorm.x, tTangente.y*tTextNorm.y + tCoTangente.y*tTextNorm.y +
tNorm.y*tTextNorm.y, tTangente.z*tTextNorm.z + tCoTangente.z*tTextNorm.z +
tNorm.z*tTextNorm.z ).Normalize();
// Here, after some research, I came across this : http://www.txutxi.com/?p=316 ,
// that allow us to convert the normal map tangent space to the object space.
The results are still not good. My main concern are the Tangent and Binormals. The Axis taken in reference (here: m_tAxisZ, the Z Axis of the Sphere), is not right. But I don't know what to take, or even if what I am doing is really good. So I came here for help.
So, we finally did it. :D Ok, I will try to be clear. For this, two images :
(1) : http://i.imgur.com/cHwrR9A.png
(2) : http://i.imgur.com/mGPH1RW.png
(My drawing skill has no equal, I know).
So, the main problem was to find the Tangent "T" and the Bi-tangent "B". We already have the Normal "N". Our circle always being at the origin with a radius of 1, a point on its surface is equal to the Normal to that point (black and red vector on the first image). So, we have to find the tangent to that point (in green). For this, we just have to rotate the vector from PI/2 rad :
With N( x, y ) :
T = ( -N.y , N.x )
However, we are in 3D. So the point will not always be at the equator. We can easily solve this problem by ignoring the position in Y of our point and normalize the vector with only the two other component. So, on the second image, we have P (we set its Y value to 0), and we normalize the new vector to get P'.
With P( x, y, z ) :
P' = ( P.x, 0, P.z).Normalize();
Then, we go back to my first message to find the T. Finally, we get the B with a cross product between the N en the T. Finally, we calculate the normal to that point by taking the normal map into account.
With the variable "Map" containing the three channels (RGB) of the normal Map, each one clamped from -1 to 1, and T, N and B all being 3D vectors :
( Map.R*T + Map.G*B + Map.B*N ).Normalize();
And that's it, you have the normal to the point taking your normal map into account. :) Hope this will be usefull for others.
You are mostly right and completely wrong at the same time.
Tangent space normal mapping use a transformation matrix to convert the tangent space normal from the texture to another space, like object or world space, or transform the light in the tangent space to compute the lighting with everything in the same space.
Bi-normal is a common mistake and should be named bi-tangent.
It is sometime possible to compute the TBN at the fly on simple geometry, like on a height-map as it is easy to deduce the tangent and the bi-tangent on a regular grid. But on a sphere, the cross trick with a fixed axis will result to a singularity at the pole where the cross product give a zero length vector.
Last, even if we ignore the pole singularity, the TBN must be normalized before you apply the matrix to the tangent space normal. You may also miss a transpose, as a 3x3 orthonormal matrix inverse is the transpose, and what you need is the inverse of the original TBN matrix if you go from tangent to object.
Because of all this, we most often store the TBN as extra information in the geometry, computed from the texture coordinate ( the url you referenced link to that computation description ) and interpolate at runtime with the other values.
Rem : there is a rough simplification to use the geometry nornal as the TBN normal but there is no reason in the first place that they match.
I've gone through a lot of questions online for the last few hours trying to figure out how to calculate the rotation between two vectors, using the least distance. Also, what is the difference between using Atan2 and using the dot product? I see them the most often.
The purpose is to rotate in the y axis only based on differences in X/Z position (Think top down rotation in a 3D world).
Option 1: use Atan2. Seems to work really well, except when it switchs the radians from positive to negative.
float radians = (float)Math.Atan2(source.Position.X - target.Position.X, source.Position.Z - target.Position.Z);
npc.ModelPosition.Rotation = new Vector3(0,
npc.ModelPosition.Rotation.Y + (radians),
0);
This works fine, except at one point it starts jittering then swings back the other way.
Use the dot product. Can't seem to make it work well, it ALWAYS turns in the same direction - is it possible to get it to swing in the least direction? I understand it ranges from 0 to pi - can I use that?
float dot = Vector3.Dot(Vector3.Normalize(source.Position), Vector3.Normalize(target.Position));
float angle = (float)Math.Acos(dot);
npc.ModelPosition.Rotation = new Vector3(0,
npc.ModelPosition.Rotation.Y + (angle),
0);
Thanks,
James
You calculate two different angles:
Angle alpha is the angle calculated with atan2 (which equals opposite side / adjacent side). Angle beta is the one calculated with the dot product.
You can achieve similar results (as with atan) with the dot product with
angle = acos(dot(normalize(target - source), -z))
(-z is the unit vector in negative z direction)
However, you will lose the sign.
I want to move a sprite in a circular motion by using a circle radius and movingangle.
I know for instance that the sprite is moving in a circle with the radius 10 and It's current position is (387,38) and angle is 28 degrees. Now I wann't move it say, 100px along the circle perimeter.
p1 = x&y coordinate, known (387,38) in example with movingangle 28 degrees
r = radius, known (10 in example)
l = distance to move along bend, known (100px in example)
p2 = new point, what is this x and y value?
I have a solution that works but I dont quite like it. It kind of don't make sense to me and it requires more calculations than I think is required. It works by first calculating the center point using p1 and then doing the same thing backwards so to speak to get p2 (using cosinus and sinus). There should be a quicker way I believe but I cant find anyone doing exactly this.
The reason i tagged cocos2d is because thats what I'm working with and sometimes game frameworks provide functions to help with the trigonometry.
Try this:
a1 = 28*(π/180)
cen.x = p1.x - r*SIN(a1)
cen.y = p2.y - r*COS(a1)
a2 = a1 + l/r
p2.x = cen.x + r*SIN(a2)
p2.y = cen.y + r*COS(a2)
It does not get any simpler than this. Move from p1 to the circle center, and then to p2.
You can do it with magic of anchor point in a really simple way =) All the transformations with any node are done relatieve to it's anchor point. Rotation is a kind of transformations too. So, you can do something like this
CGFloat anchorY = neededRadius / spriteHeight;
[yourSprite setAnchorPoint:ccp(0.5f, anchorY)];
[yourSprite setPosition:neededPosition]; //remember that position is also for anchor point
[self addChild: yourSprite];
[yourSprite runAction:anyRotateAction]; // i mean, CCRotateBy or CCRotateTo
Calculate the origin, and then use polar co-ordinates to move the point along the circle.