What I'm trying to do:
Create a compass when given a direction in degrees (0 - 360). Like so:
What I have done:
I have managed to get the SVG image to point in the right direction but I can't seem to get it to rotate around the circle. To attempt a solution around the circle, I have decided to try get the positioning using the ellipse tool and this formula. This is what it looks like at the moment:
(notice how the arrow faces a different direction to the ellipse, in the circle, on the axis - given the center point is the middle of the green circle)
void setDirection(float value, int radius) {
fill(secondaryColour);
float origin_x = (1280 - (width-400)/2);
float origin_y = height/2;
float x = origin_x + radius * cos(radians(value));
float y = origin_y +radius * sin(radians(value));
//grey circle
ellipse(x, y, 20, 20);
//arrow SVG
pushMatrix();
translate(200, 300);
rotate(radians(value));
scale(0.5);
shape(arrow);
popMatrix();
}
Please note: value is in degrees and radius is the radius I want the arrow to sit on. What am I doing wrong with the ellipse? and how can I bring them both together?
I found that the starting angle started on the white line, but I was assuming it would start on the red (similar to the angle of the arrow). To resolve the issue I needed to subtract 90 degrees from the variable value before converting it into radians.
Related
I am rotating a shape in Java using g2D.rotate(Math.toRadians(rotationDegrees), x, y) where the x and the y are the axis that I want the shape to be rotating along. When I try to move the shape with a key listener, the shape still moves up, down, left, and right instead of moving according to the angle that it is facing. Is there any way that I can rotate a shape and have it move in the same direction that it is facing?
You need to do a little bit of trigonometry before you change your x and y values. Maintain a variable angle which stores the total amount that your shape has rotated from its initial position.
Now suppose you've got variables xChange and yChange that represent the desired change in x and y, but in the reference frame of the shape, not of the frame. Then instead of writing x += xChange; you'd write
x += (xChange * Math.cos(angle) + yChange * Math.sin(angle));
and instead of writing y += yChange; you'd write
y += (yChange * Math.cos(angle) - xChange * Math.sin(angle));
This assumes that angle is stored in radians, not degrees. You may also need to experiment with the + and - signs, depending on whether you're measuring y from top to bottom or bottom to top.
I have a my rectangle.
The application generates another rectangle.
It can be more smaller or larger than my rectangle.
How can I tell when its rect near of the mine using their X, Y, Weight and Hight?? I do not want to know if is into my rectangle.
Draw 1 or more non-visible shapes that are relative to your rectangle's position that fit your definition of "near", then check to see if these shape(s) intersect with the application-generated rectangle in question.
For example, one way you might implement this is drawing a non-visible rectangle that surrounds your rectangle, then checking to see if the surrounding rectangle intersects with the application-generated rectangle.
I found the solution!
I have calculated the middle point of my rectangle.
If the rectangle generated have into the point, is near!
You can use the Math formula to calculate the distance between two points like this:
double getDistance(int x, int y, int x2, int y2) {
double distance;
distance = Math.sqrt( Math.pow( Math.abs(x2 - x) , 2 ) + Math.pow( Math.abs(y2 - y) , 2 ) );
return distance;
}
Im developing simple game. I have cca. 50 rectangles arranged in 10 columns and 5 rows. It wasn't problem to put them somehow to fit the whole screen. But when I rotate the canvas, let's say about 7° angle, the old coordinates does't fit in the new position of the coordinates. In constructor I already create and define the position of that rectangles, in onDraw method I'm drawing this rectangles (of course there are aready rotated) bud I need some method that colliding with the current rectangle. I tried to use something like this (i did rotation around the center point of the screen)
int newx = (int) ((x * Math.cos(ROTATE_ANGLE) - (y * Math.sin(ROTATE_ANGLE))) + width / 2);
int newy = (int) ((y * Math.cos(ROTATE_ANGLE) + (x * Math.sin(ROTATE_ANGLE))) + height / 2);
but it doesn't works (becuase it gives me absolute wrong new coordinates). x and y are coordinates of the touch that I'm trying to calculate new position in manner of rotation. ROTATE_ANGLE is the angle of rotation the screen.
Does anybody know how to solve this problem, I already go thorough many articles, wiki, wolframalpha categories but not luck. Maybe I just need some link to understand problem more.
Thank you
You use a rotation matrix.
Matrix mat = new Matrix(); //mat is identity
mat.postRotate(ROTATE_ANGLE); //mat is a rotation matrix of ROTATE_ANGLE degrees
float point[] = {10.0, 20.0}; //create a new float array representing the point (10, 20)
mat.mapPoints(point); //rotate the point by the requested amount
Ok, find the solution.
First it is important to convert from angle to radian
Then I personly need to negate that radian value.
That's all, this solution is correct
I'm making a game in slick2D and i wanted the game to adjust resolutions acording to the screen being used, so when i initialize my AppGameContainer i call it as the following shows
AppGameContainer appgc;
try{
appgc = new AppGameContainer(appgc.getScreenWidth(), appgc.getScreenHeight(), true);
}catch....
So my question is when using this method will i have to draw objects and do collision detection using appgc.getScreenWidth() and appgc.getScreenWidth() for example if i wanted to draw a square in the middle of the screen would i have to do it like so ?
g.fillRect(appgc.getScreenWidth, appgc.getScreenHeight(), 50, 50);
or is there some easier way around this, as this could get confusing with collision later on as opposed to just an X and a Y axis?
thanks for any helpful answers in advance.
If you want to draw a square in the middle of the screen using fillRect you have to do that :
int square_width = 50 ;
int = 50 ;
g.fillRect((appgc.getScreenWidth+square_width)/2, (appgc.getScreenHeight+square_height)/2, square_width, square_height);
Indeed there is the function :
fillRect(float x1, float y1, float width, float height)
x1 - The x coordinate of the top left corner
y1 - The y coordinate of the top left corner
width - The width of the rectangle to fill
height - The height of the rectangle to fill
In java I have three points denoting two line with making an angle. Now I have to create the angle arc about 10 pixel apart from common point. with showing angle on the arc. I am able to calculate the angle but how to draw the arc and to show the angle on the arc. Please tell me some code view or link where I can find solution for this. CODE snippet is as below.
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
Point p1=new Point(100,100);
Point p2=new Point(200,100);
Point p3=new Point(100,0);
Line2D line1=new Line2D.Double(p1, p2);
Line2D line2=new Line2D.Double(p1, p3);
g2.draw(line1);
g2.draw(line2);
double angle=getAngle(line1,line2);
System.out.println(angle);
//g2.drawArc(110, 100, 20, 20, 100, 30);
}
public double getAngle(Line2D line1,Line2D line2){
double angle1=Math.atan2(line1.getY1()-line1.getY2(), line1.getX1()-line1.getX2());
double angle2=Math.atan2(line2.getY1()-line2.getY2(), line2.getX1()-line2.getX2());
return Math.toDegrees(angle1-angle2);
}
I don't know how to use DrawArc to draw exact arc which i want and also to put the angle on that.
Thanks & Regards.
From the documentation drawArc's arguments are:
int x, int y, int width, int height, int startAngle, int arcAngle
the x and y are your common point (p1), and your width and height are probably both 10 (to draw a circular arc with radius 10pixels)
The angle that you have computed is the last argument (arcAngle) which measure the sweep of the arc in a counter-clockwise direction. So that last part you need to work out is the start angle, which is probably your angle1 or angle2 (0 in this case is the positive x-axis or 3 o'clock position).
Keep in mind, as written you will sometimes be drawing an arc of > 180 degrees, you will need more logic if you want to always find the smallest angle between the two lines.
As for the text you can use drawString and figure out the x and y using some trigonometry with half your sweep angle and your desired radius. Though for optimal placement you might need to figure out what quadrant you are drawing in and adjust from there.