How to change the center of the trackball in its regular implementation? - java

I have successfully implemented a trackball in java using the below two tutorials:
http://nehe.gamedev.net/tutorial/arcball_rotation/19003
http://www.java-tips.org/other-api-tips/jogl/arcball-rotation-nehe-tutorial-jogl-port.html
This trackball is centered at the screen center. Now, I wish to center it at any point on the screen. How can I incorporate the center logic in the existing code? How the normalization of mouse coordinates change due to introduction of center?

This can be easily achieved by transforming the mouse coordinates. When you transform the mouse coordinates as you do at the start of the first tutorial, instead of just transforming them as specified to the range -1..1, first subtract the offset from the centre of the screen where you want to make your trackball centre. So to make the trackball centre 50 pixels to the right and 20 pixels above the centre, use
MousePt.X = ((MousePt.X-50) /Width)*2)-1;
MousePt.Y = ((MousePt.X-20) /Height)*2)-1;

Related

How to zoom in and move a map of multiple squares?

I have 10x10 sqares that are forming a map. The variable zoom, xPos and yPos are defining how deep I am scolling in the map and the position of the camera.
Each tile has a x and y - coordinate (0-9).
How can I display this map?
I've tried to do this:
rect(xzoom+xPos, yzoom+xPos, zoom, zoom); //the function rect makes a rectangle with the center at the first 2 inputs)
The problem is that I'm always zooming in the upper left corner;
I've also tried this:
rect((x-5.5)*zoom+xPos, (y-5.5)*zoom+yPos);
but this zooms always in the center in the map while I want it to zoom in the center of the screen.
Please help me
I really suggest sitting down with some graph paper and a pencil. Draw out a bunch of example grids with their coordinates and sizes. Then draw out what they look like at different zoom level until you notice a pattern. If you can't get that pattern to work, please post an MCVE and we'll go from there.
Also note that Processing has a scale() function that might come in handy. More info is available in the reference.

Java - Screen Coordinates to Image Coordinates

I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.
However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.
How can I determine the image's placement on the screen (not relative to anything)?
I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.
Surely there must be a way to get the absolute pixel location on the screen of a component?
If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want
And yes as #RealSkeptic mentioned in comments to question:
SwingUtilities.html#convertPointFromScreen
will do all this work for you considering components hierarchy

Draw curved custom object in LIBGDX?

I've recently been looking into LibGDX and seem to have hit a wall, seen in the picture, the blue dot represents the users finger, the map generation it self is where i seem to get stuck, does LibGDX provide a method of dynamically drawing curved objects? I could simply generate them myself as images but then the image is hugely stretched to the point of the gap for the finger can fit 3! But also would need to be 1000's of PX tall to accommodate the whole level design.
Is it such that i should be drawing hundreds of polygons close together to make a curved line?
On a side not i'll need a way of determining when the object has from bottom to top so i can generate another 'chunk' of map.
You don't need hundreds of polygons to make a curve like you drew. You could get away with 40 quads on the left, and 40 on the right, and it would look pretty smooth. Raise that to 100 on each side and it will look almost perfectly smooth, and no modern device is going to have any trouble running that at 60fps.
You could use the Mesh class to generate a procedural mesh for each side. You can make the mesh stay in one spot, locked to the camera, and modify it's vertices and UVs to make it look like you are panning down an infinitely long corridor. This will take a fair amount of math up front but should be smooth sailing once you have that down.
Basically, your level design could be based on some kind of equation that takes Y offset as an input. Or it could be a long array of offsets, and you could use a spline equation or linear equation to interpolate between them. The output would be the UV and X coordinates which can be used to update each of the vertices of your two meshes.
You can use the vertex shader to efficiently update the UV coordinates, using a constant offset uniform parameter that you update each frame. That way you don't have to move UV data to the GPU every frame.
For the vertex positions, use your Mesh's underlying float[] and call setVertices() each frame to update it. Info here.
Actually, it might look better if you leave the UV's and the X positions alone, and just scroll the Y positions up. Keep a couple quads of padding off top and bottom of screen, and just move the top quad to the bottom after it scrolls off screen.
How about creating a set of curved forms that can be put together variably. Like the gap in the middle will at the top and bottom of each image be in the middle (with the same curvature at end and beginning points)...
And inbetween the start and end points you can go crazy on the shape.
And finally, you can randomly put those images together and get an endless world.
If you don't want to stop in the middle each time, you could also have like three entry and exit points (left, middle, right)... and after an image that ends left, you of course need to add an image that starts left, but might end somewhere else...

How to rotate a single shape in 3d space in opengl?

so I'm pretty new with opengl and creating 3d shapes. So for my example I have two squares, one with a height/width 2 with the center at the origin coordinate (0,0,-10), and one that is to the far left side of the window. I am trying to rotate the square that lies in the origin along the x-z plane without rotating the square that is located to the far left side of the screen. My approach to this was to save each xyz coordinate of the center square to a variable, and creating a method that uses the behavior of cos(theta) to rotate the square along the x-z plane. My code works, but I assume this is a horrible approach as there must be some more efficient method that is already created that can do the same functionality. I looked at glRotatef(), but from what I understood this only rotates my camera view which in the end would rotate both the middle square and the far left square whereas I only want to rotate the middle square. Is there some other method that already exists that can easily rotate a single 2d shape in 3d space?
In case its relevant, I have included the rotating code I made myself for the middle square: (btw the blue class is just some class I made that has the squares coordinates and the circle degree for cos(theta))
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
blue.setCircle(blue.getCircle()+1f);//getCircle is initially zero and gets incremented by 1 for everytime the program loops with the user holding the left button.
blue.setXfrontTR((float)Math.cos(Math.toRadians(blue.getCircle())));//Changing top-right x coordinate of the middle square
blue.setZfrontTR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f)))); //Changing top-right z coordinate of the middle square.
blue.setXfrontTL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontTL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing top-left x,z coordinates
blue.setXfrontBL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontBL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing bottom-left x,z coordinates
blue.setXfrontBR((float)Math.cos(Math.toRadians(blue.getCircle())));
blue.setZfrontBR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f))));//Changing bottom-right x-z coordinates
}
If you give each object that requires independent movement a model-view matrix you can achieve this. The other option to quickly draw/move a few independent objects is to:
for each object:
pushMatrix()
draw object
popMatrix()
while in the modelview matrix...
The method of drawing depends greatly on the OpenGL version you're coding to but the above will work for simple drawing. I'm not an expert on OpenGL / 3D programming so, if you wait a bit you may hear(see) better wisdom than what I offer :)

Putting an angle on image view

I'd like to set an angle on an image view and have that angle be generated randomly, and set the ancho point of the image view to the center of the android screen. After that I'd like the computer to generate a spot from a certain distance of the middle to the end of the screen on that angle and set a button to appear there. I'm not sure if eclipse has a quick automatic way to do that.
Thanks.
I've added a picture to help. I'd like the arrow to point to a random angle and then a button to appear on that angle but outside of the circle (the circle is imaginary just showing that it needs to appear outside of a certain distance from the center.
You can use setRotation method for rotate a view in android.
image.setRotation(90); // instead of 90 you can give your generated value.
// But make sure the value should be float.
image.setRotationX(90); // if you want rotate depends x axis
image.setRotationY(90); // if you want rotate depends y axis
also take a look on here
I hope this will help you.

Categories

Resources