As describes the Methane image, I need to make a Tetrahedron in Java3d with the angles between any two bounds to be 109 deg (H are bounded with C, so the lines are the bounds).
I must admit that I don't quite have any idea about how should I do it. I tried it that way: one bound with rotX and rotZ at 0 deg both. The one rotX at 109 deg. The next step would be rotZ and rotX at 109 deg. Let's say that angle for rotX is AngleX and the angle for rotZ is AngleZ. The end of my cylinder will be (calculated and tried) at (sin AngleZ,cos AngleZ*cos AngleX, cos AngleZ*sin AngleX). This is legit, so ouviously rotating again would do no good.
I have 4 cylinders of 2f length, created one at a time, translated at (0,1,0) so their end is at (0,0,0). From this point, I rotate my cylinders around the point (0,0,0) and I try to achieve a tetrahedron. The left cylinder is at {109,0} and the right one at {109,109} Can be easily seen that it's not the same angle between the left cylinder and the one going up and the right one and the cylinder going up
Also I have to say that solving this by calculus won't help me too much, because I will probably need to change angles in some situation, but I still need it to have same angles between cylinders.
Any ideas? How should I solve this?
Note: To be easier, I transformed from radians to degrees in my program so I work with degrees.
I think you may be making this a bit more complicated than it needs to be. Isn't methane symmetrical? If so, put the hydrogen atoms at non-adjacent vertices of a cube, and the carbon at the center of the cube.
To see what I mean do a Google search for "methane symmetry" and look at the images.
If you need a non-symmetrical variant, I would still calculate the locations of the atoms outside your program, or using non-graphical code, and only use java-3d to place them.
Related
So I'm making a TD game where I can place a gatling gun, and depending on which sector of a surrounding circle the mouse is in, the sprite and bullet path will change.
My difficulty is with creating an algorithm which will tell me which sector my mouse is in.
My circle has 16 sectors, and a radius of 300. Each arc has a length of 117.81.
Extending from (300,300), I have an exact list of all the coordinates of the lines, so I am able to currently draw the sector like this:
Circle
I'm using a mouse listener to detect the coordinates of my mouse whenever my mouse moves, so I have a "currentPoint" to check within which sector it's in.
Based on this information, can anyone think of an easy way to simply return an integer of which sector the mouse is currently inside? Preferably somewhat efficiently.
These are the two ways I'm thinking about how it would look:
Two_Ideas
And I did look at this StackOverflow which seemed like a similar problem: Efficiently find points inside a circle sector
And I implemented it with Java, but it doesn't seem to translate without having Vectors and I'm a bit too confused about the math to make it work.
Been trying to figure this out for a while, I would love any help with an implementation of any kind, (don't mind adding Trig calculations), along with any help understanding the problem.
Thank you!!
To get sector, you need to get angle relative to point center.
Pseudocode (I am not sure how math functions and rounding look in Java):
double angle = math.atan2(mouse.y-center.y, mouse.x-center.x);
angle = angle - math.pi / 16.0;
while (angle < 0) {
angle = angle + 2*math.pi;
}
sector = math.floor(angle * 8.0 / math.pi);
I made correction by half-sector becouse your first sector is centered around OX axis.
I've created a simple planetary simulation where a planet orbits a star.
The code for the orbit is this:
a = a + vel * delta;
planetX = Math.cos(a) * orbitRadius + parentStar.getX();
planetY = Math.sin(a) * orbitRadius + parentStar.getY();
Now that works just fine, but my problem is that the orbit is not from the center of the planet around the center of the star.
This is what happens
As you can see, the first red dot on the small circle is the Position of the planet wich orbits around the second small red dot, this is because the circle is drawn from (0,0), so both the planets (0,0) circles around the (0,0) of the star.
I need the the center of the planet to circle the stars center, not their origin point.
Is there a good fix for this?
Your calculation of the orbit is fine. The only problem seems to be that you treat "position" differently when calculating orbits and when drawing the planets: When you draw them, you treat x and y as one of the corner points, but when you calculate the oribit, you treat them as the centre of the body. The simplest way would be to change the visualisation, not the calculation.
Since you did not post the code you use to draw the shapes, I can only guess, but I assume it looks somewhat like this (obviously Pseudocode):
for (Planet p : starsAndPlanets) {
drawCircle(p.x, p.y, p.radius * 2, p.radius * 2);
}
Change this to something like this:
for (Planet p : starsAndPlanets) {
drawCircle(p.x - p.radius, p.y - p.radius, p.radius * 2, p.radius * 2);
}
This way, x and y are the position of the centre of the planet, and with p.x - p.radius and p.y - p.radius you get the corner point. Of course, you could in a similar way change all your orbital mechanic formulas to calculate the centre from the corner point, but IMHO it is much simpler and more natural to treat x and y as the centre.
For now the most suitable way I can think of is getting the star's world coordnates and passing them every frame to the child's coordinates. As you do so, the child would have the same coordinates everyframe.
The next part is translating it and rotating it around the Star - the way you can achieve that is by setting the planet's position to be transposed by the Star's position with a sin(x)*cos(x).
Let me show you an example:
planet[0] = star[0] + sin(angle)*scale
planet[1] = star[1] + cos(angle)*scale
Where the angle would change incrementally and the scale will just shift the child object further from its parent, keeping it a constant (or modifying it if you wish) thus increasing the radius from its 'new' center.
I know some people may mention matrices or other types of transformations, but for this situation I think the above solution would be most relevant and cleanest in my opinionp
The way it works is you take the parent's 'WORLD coordinates' and set them to be the child's. By modifying the Scale value you increase the distance of the object from the center (so they won't overlap) and you multiply this with the sin and cos of the angle you specified to make it rotate.
P.S. Keep in mind that if you're dealing an FPS-dependant engine to render, the more FPS the faster the simulation will be, and vice-versa, because if you render at 1000 fps, this means you execute your code 1000 times, compared to 100 for example. Therefore, you will increment the angle 1000 times or 100 respectively. If you have this issue, try setting a constant framerate if you can - it's the simplest workaround for lightweight simulations.
Edit: I forgot to mention that the concept works for all objects in your case. You just have to work our the relationships and use the function for eqch object seperately where each object has a position and angle of orbit (if it orbits around a different object).
I am programming a Minecraft Bukkit plugin and need a way to calculate an input number from 0 to 360 for displaying a custom compass. So if the player directly looks at the object (shouldn't handle viewing height or position height), this number would be 0 and if the player's back is looking on the object it would be 180.
I already successfully calculated both numbers I need:
The absolute looking angle of the player. Is 0 when the player looks in north direction and 180 in south direction.
The location angle between the player's position and the object's position. Using Math.atan2 to get the angle between [X, Z] of these locations.
Both values seems to be calculated correctly. But I can't find out what to do to get the number I described at first. Tried substraction, addition. Any ideas?
It should be the difference between them - if the player is looking north, and the object delta x, delta z gives a bearing of 45 degrees then the needle should be in front and to the right at 45 (=45-0), if the player is looking south and tho object x,z is 45 then the needle should be behind and to the left at 225 or -135 (=45-180).
Check that you've converted the result of Math.atan2 to degrees so you're subtracting values in the same units, and that the axes conventions are consistent. This says that +ve x-axis is east and +ve z-axis is south. A bearing of 0, North is given by atan2(0,1), which implies that you should be using Math.toDegrees(Math.atan2(deltaX, -deltaZ)) to get the bearing.
When doing these sort of things, it's much easier to write up half a dozen unit tests which cover the cases and play with the signs to see what the effects are.
I am developing an augmented reality application for android and trying to use openGl to place cubes at locations in the world. My current method can be seen in the code below:
for(Marker ma: ARData.getMarkerlist().values()) {
Log.d("populating", "");
gl.glPushMatrix();
Location maLoc = new Location("loc");
maLoc.setLatitude(ma.lat);
maLoc.setLongitude(ma.lng);
maLoc.setAltitude(ma.alt);
float distance = currentLoc.distanceTo(maLoc);
float bearing = currentLoc.bearingTo(maLoc);
Log.d("distance", String.valueOf(distance));
Log.d("bearing", String.valueOf(bearing));
gl.glRotatef(bearing,0,0,1);
gl.glTranslatef(0,0,-distance);
ma.cube.draw(gl);
gl.glPopMatrix();
}
gl.glRotatef(y, 0, 1, 0);
gl.glRotatef(x, 1, 0, 0);`
Where y is yaw and x is the pitch. currently I am getting a single cube on the screen at a 45 degree angle someway in the distance. It looks like I am getting sensible bearing and distance values. Could it have something to do with the phones orientation? If you need more code let me know.
EDIT: I updated bearing rotation to gl.glRotatef(bearing,0,1,0); I am now getting my cubes mapped horizontally along the screen at different depths. Still no movement using heading and pitch but #Mirkules has identified some reasons why that might be.
EDIT 2: I am now attempting to place the cubes by rotating the matrix by the difference in angle between heading and bearing to a marker. However, all I get is a sort of jittering where the cubes appear to be rendered in a new position and then jump back to there old position. Code as above except for the following:
float angleDiff = bearing - y;
gl.glRotatef((angleDiff),0,1,0);
gl.glTranslatef(0,0,-distance);
bearing and y are both normalised to a 0 - 360 scale. Also, I moveed my "camera rotation" to above the code where I set the markers.
EDIT 3: I have heading working now using, float angleDiff = (bearing + y)/2;. However, I cant seem to get pitch working. I have attempted to use gl.glRotatef(-x,1,0,0); but that doesn't seem to work.
It's tricky to tell exactly what you're trying to do here, but there are a few things that stick out as potential problems.
Firstly, your final two rotations don't seem to actually apply to anything. If these are supposed to represent a movement of the world or camera (which mostly amounts to much the same thing) then they need to happen before drawing anything.
Then your rotations themselves perhaps won't entirely do what you intend.
Your cube is rotated around the Z axis. The usual convention in GL is for the camera to look down the Z axis, with the Y axis being considered 'up'. You can naturally interpret axes however you like, but a rotation around 'Z' would not typically be 'bearing', but 'roll'. 'Bearing' to me would be analogous to 'yaw'.
As you translate along the Z axis, I assume you are trying to position the object by rotating and translating, but obviously if the rotation is around the same axis as you translate along, it won't actually alter the position of the cube - it will always just be directly in front of the camera, spinning on its axis.
I'm not really clear on why you're trying to position the cube like that when it seems like you start off with a more specific location. You could probably directly construct a more appropriate matrix.
Finally, your camera/world rotation is two concatenated rotations around Y and X. You call these pitch and roll, but typically using euler angles for a camera rotation does not result in an intuitive result where terms like pitch and roll make complete sense. It is common to maintain an orientation and apply individual rotations to that in order to update it, rather than attempting to update several dependent rotations.
So yes, I would expect that this code, in the absence of other matrix operations, would likely result in drawing one or more cubes straight ahead which are simply rotated by some angle around the view direction.
I've been searching a lot on this problem, but I couldn't really find an answer that would fit.
I need to rotate a cylinder around a given point (eg, 0,0,0), but the pivot of the cylinder is given by default. How do i change that?
I found this topic, and it's quite what I would want to do, but I don't know how to do it with java.
To explain better what I would like to do, I'll show 3 images.(v)
imageshack.us/photo/my-images/259/aintgood.jpg
imageshack.us/photo/my-images/840/whatineed.jpg
imageshack.us/photo/my-images/705/nogoodn.jpg
So, the first image shows my basic problem, the cylinder should be positioned with the end at the center of the sphere, let's say (0,0,0). The user gives two angles. The first one is for a rotX command, the second one for a rotZ one. The pivot of the cylinder is at its center, so, as image 3 shows, even if i translate the cylinder so its end is at the center of the sphere, when it rotates, the whole thing ruins.
Image 2 shows what the cylinder-sphere group should look like, regardless the given angles.
The image is not obtained based on an algorithm, but based on calculus, and mouserotated.
The general procedure for rotation about an arbitrary point P is:
Translate by -P (so P is at (0, 0, 0))
Rotate around the origin
Translate by P (to bring the origin back to the original location of P)
The easiest way to do this is to represent everything in homogeneous coordinates and represent translations and rotations by matrices. Composing the above three transformations (translate-rotate-translate) is done by matrix multiplication. If the rotation is composed of two or more simpler rotations, then the rotation matrix itself is a product of the matrices for the simpler rotations.