I have defined a camera that rotates around the scene's origin, with a Rotate X axis, and a Rotate Y axis.
I would like that the X axis defines the "height" of my Y axis camera so that it revolves around the given latitude on the sphere.
I have defined the rotates as such:
_cameraTranslate = new Translate(0, 0, -10);
_cameraRotateX = new MyRotate(0, 0, 0, 10, Rotate.X_AXIS);
_cameraRotateY = new MyRotate(0, 0, 0, 10, Rotate.Y_AXIS);
_camera.getTransforms().addAll(_cameraTranslate, _cameraRotateX, _cameraRotateY);
And rotate them as such:
private void onMouseDragged(MouseEvent e) {
_cameraRotateX.setAngle360(_cameraRotateX.getAngle() - (-e.getSceneY() + _mouseOldY));
_cameraRotateY.setAngle360(_cameraRotateY.getAngle() - (e.getSceneX() - _mouseOldX));
_mouseOldX = e.getSceneX();
_mouseOldY = e.getSceneY();
}
This is what it does when I rotate around, shifting the world instead of keeping the X axis lines parallel to the frame if it makes sense.
I realized I have to modify the axis of the X camera (the one going up and down). As the camera rotates around the Y axis, the X and Z axes move so the rotation needs to be adapted accordingly.
Instead, I added a Point3D that I modify every time the Y camera is moved:
_cameraRotateXAxis = new Point3D(1, 0, 0);
_cameraRotateX = new MyRotate(0, 0, 0, 10, _cameraRotateXAxis);
And in the mouseDragged method:
private void onMouseDragged(MouseEvent e) {
_cameraRotateX.setAngle360(_cameraRotateX.getAngle() - (-e.getSceneY() + _mouseOldY));
_cameraRotateY.setAngle360(_cameraRotateY.getAngle() - (e.getSceneX() - _mouseOldX));
_cameraRotateXAxis = _cameraRotateXAxis.add(-_cameraRotateXAxis.getX(), -_cameraRotateXAxis.getY(), -_cameraRotateXAxis.getZ());
double angle = _cameraRotateY.getAngle();
double newDelta = _cameraRotateY.getAngle()%90 / 90d;
if (angle >= 0 && angle < 90) {
_cameraRotateXAxis = _cameraRotateXAxis.add(-(1 - newDelta), 0, newDelta);
}
else if (angle >= 90 && angle < 180) {
_cameraRotateXAxis = _cameraRotateXAxis.add(newDelta, 0, 1 -newDelta);
}
else if (angle >= 180 && angle < 270) {
_cameraRotateXAxis = _cameraRotateXAxis.add(1 - newDelta, 0, -newDelta);
}
else {
_cameraRotateXAxis = _cameraRotateXAxis.add(-newDelta, 0, -(1 - newDelta));
}
_mouseOldX = e.getSceneX();
_mouseOldY = e.getSceneY();
}
Related
I have an image that I rotate before I draw. The image is rotated by the angles of a hexagon. In other words, the image basically "highlights" the individual edges of a hexagon. I need to detect if the mouse was clicked inside of this rotated image. Detecting a mouse click inside of an unrotated image is pretty simple, but I have no idea about how to detect clicks within rotated points. Is there a way to get the points of the image's corners after rotation so I can place an invisible polygon on top of the image and use Polygon.contains()?
Image highlightEdge = new Image("assets/img/highlightEdge.png");
if(angle == 90){
highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(testPoint.x - 56, testPoint.y);
} else if(angle == 210) {
highlightEdge.setCenterOfRotation(0, 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(lastSettlement.x - 72, lastSettlement.y - 32);
} else if( angle == 330){
highlightEdge.setCenterOfRotation(0, 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(lastSettlement.x - 8, lastSettlement.y - 32);
} else if(angle == 30){
highlightEdge.setCenterOfRotation(0, 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(lastSettlement.x-8, lastSettlement.y);
} else if(angle == 150){
highlightEdge.setCenterOfRotation(0, 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(lastSettlement.x-72, lastSettlement.y);
} else {
highlightEdge.setCenterOfRotation(0, 0);
highlightEdge.rotate(new Float(angle));
highlightEdge.draw(lastSettlement.x-40, lastSettlement.y - 48);
}
You could create a Shape to exactly match the shape of the Image, and then use its method contains to detect if the mouse was clicked inside.
To take in consideration the rotation of the Image you could apply a corresponding rotation Transform to the Shape.
I created the method shapeFromImage that does this; it receives an Image and its position and returns the corresponding Shape:
/**
* Returns the Shape of an Image considering its rotation
* #param image
* #param x the x position of the Image
* #param y the y position of the Image
*/
public static Shape shapeFromImage(Image image, float x, float y) {
// create a rectangle with same position and size of the image
Shape imageShape = new Rectangle(x, y, image.getWidth(), image.getHeight());
// get the rotation angle of the image
float angle = image.getRotation();
// if the image is rotated, we also need to rotate our shape
if (angle != 0.f) {
// convert the rotation angle in radians to use in Transform
float angleInRadians = (float) Math.toRadians(angle);
// get the point of rotation to use in Transform.
// image.getCenterOfRotation returns a point relative to the image.
// for Transform we need an absolute point, so we add the image position to it
float rotationX = image.getCenterOfRotationX() + x;
float rotationY = image.getCenterOfRotationY() + y;
// create the rotation Transform to match the image rotation
Transform rotationTransform = Transform.createRotateTransform(angleInRadians, rotationX, rotationY);
// apply the rotation Transform to our shape
imageShape = imageShape.transform(rotationTransform);
}
return imageShape;
}
In your example you could use it like this:
float positionX;
float positionY;
if (angle == 90) {
highlightEdge.setCenterOfRotation(highlightEdge.getWidth(), 0);
highlightEdge.rotate(new Float(angle));
positionX = testPoint.x - 56;
positionY = testPoint.y;
highlightEdge.draw(positionX, positionY);
}
...
// you can now use this Shape to use its method "contains"
imageShape = shapeFromImage(highlightEdge, positionX, positionY);
I'm trying to rotate a TextureRegion around its centre, however whenever I try to rotate it the rotation point is either the bottom corners of the Texture Region or a far part of the screen.
this is the update method in my object class(the Texture Region will be emulating the movements of this Object.
public void update(float delta) {
if (velocity.x < 0) {
rotation -= 50*delta;
if (rotation > 25) {
rotation = 25;
}
}
if (velocity.x > 0){
rotation += 50*delta;
if (rotation > 25) {
rotation = 25;
}
}
}
this where I call the draw method to bring the Texture Region to screen
batcher.draw(AssetLoader.saum, sam.getX(), (gameHeight - (gameHeight / 3)), -(sam.getWidth()), (gameHeight - (gameHeight / 3)), -(sam.getWidth()), -(sam.getWidth()), 1, 1, sam.getRotation());
Use one of the batch.draw methods that has originX and originY parameters.
originX and originY should be set to width/2 and height/2.
See methods here.
I've implemented a particle system. I'm drawing their textures on billboards that should be rotated towards the camera.
This works fine except for the case when the angle between particle->camera and the normal comes near to 180 degrees. Then the particle starts rotating around itself many times.
The angle is calculated using cos(angle) = dot(a, b) / (length(a) * length(b), the length are both 1 cause the Vectors are normalized.
The axis is calculated using the cross product of those two vectors.
glDisable(GL_CULL_FACE);
//calculate rotation
Vector3f normal = new Vector3f(0, 0, 1);
Vector3f dir = Vector3f.sub(new Vector3f(GraphicsData.camera.x, GraphicsData.camera.y, GraphicsData.camera.z), new Vector3f(x, y, z), null);
if(dir.length() == 0)
{
glEnable(GL_CULL_FACE);
return;
}
dir = (Vector3f) dir.normalise();
float angle = (float) Math.toDegrees(Math.acos(Vector3f.dot(normal, dir)));
Vector3f rotationAxis = Vector3f.cross(normal, dir, null);
rotationAxis = (Vector3f) rotationAxis.normalise();
System.out.println("Angle: + " + angle + " Axis: " + rotationAxis);
glBindTexture(GL_TEXTURE_2D, ParticleEngine.particleTextures.get(typeId).texture.getTextureID());
glColor4f(1f,1f,1f, time >= lifeTime - decayTime ? ((float)lifeTime - (float)time) / ((float)lifeTime - (float)decayTime) : 1f);
shaderEngine.createModelMatrix(new Vector3f(x, y, z), new Vector3f(angle * rotationAxis.x, angle * rotationAxis.y, angle * rotationAxis.z), new Vector3f(sx, sy, sz));
shaderEngine.loadModelMatrix(shaderEngine.particle);
glCallList(ParticleEngine.particleTextures.get(typeId).displayListId + textureIndex);
glEnable(GL_CULL_FACE);
What am i doing wrong calculating the rotation?
public static void createModelMatrix(Vector3f pos, Vector3f rot, Vector3f scale)
{
GraphicsData.camera.modelMatrix = new Matrix4f();
GraphicsData.camera.modelMatrix.setIdentity();
GraphicsData.camera.modelMatrix.translate(pos);
GraphicsData.camera.modelMatrix.rotate((float) Math.toRadians(rot.x), new Vector3f(1,0,0));
GraphicsData.camera.modelMatrix.rotate((float) Math.toRadians(rot.y), new Vector3f(0,1,0));
GraphicsData.camera.modelMatrix.rotate((float) Math.toRadians(rot.z), new Vector3f(0,0,1));
GraphicsData.camera.modelMatrix.scale(scale);
}
More a long comment or perhaps a partial answer to the problem:
If you are computing the cross product anyway, then use that
norm( a × b ) = sin(angle) * norm(a)*norm(b)
dot(a,b) = cos(angle) * norm(a)*norm(b)
to determine
angle = atan2( norm(a×b), dot(a,b) )
My goal is to have an image face wherever the mouse cursor is, here is my code for obtaining the rotation angle (within my Ship class) :
public void reOrient() {
Point m = MouseInfo.getPointerInfo().getLocation();
// getBoard().getPanel() is the JPanel on which the image will be drawn
Point c = getBoard().getPanel().getLocationOnScreen();
int x = m.x - c.x, y = m.y - c.y;
float angle = (float) (Math.atan2(getY() - y, x - getX()));
setOrientation(angle);
}
Then within my paint method on my JPanel :
AffineTransform reset = new AffineTransform();
reset.rotate(0, 0, 0);
g2d.rotate(ship.getOrientation(), ship.getX() + 26,
ship.getY() + 26);
g2d.drawImage(ship.getImage(), ship.getX(), ship.getY(), this);
g2d.setTransform(reset);
My issue is that when I move my mouse counterclockwise the image rotates clockwise...any ideas?
How to rotate the camera when using class VideoCapture on OpenCV? (Sample Face Detection on Android).
I'm rotating the canvas with:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Matrix matrix = new Matrix();
matrix.preTranslate(
(canvas.getWidth() - bmp.getWidth()) / 2,
(canvas.getHeight() - bmp.getHeight()) / 2);
matrix.postRotate(270f, (canvas.getWidth()) / 2,
(canvas.getHeight()) / 2);
canvas.drawBitmap(bmp, matrix, null);
}
but image from Camera doesn't rotate: Face Detect dont work.
The camera receives the stream from the following:
protected Bitmap processFrame(VideoCapture capture) {
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mGray,
Highgui.CV_CAP_ANDROID_GREY_FRAME);
UPDATE
I did the following:
#Override
protected Bitmap processFrame(VideoCapture capture) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Core.flip(mRgba.t(), mRgba, 0);
}
else {
}
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mDetect_thread.mGray,
Highgui.CV_CAP_ANDROID_GREY_FRAME);
But is dont work. When I run the program in portret orientation(on android device)- program don't start When i run the rogram in landscape orientation - programm work, but when i rotation the device, program work, but image on display dont rotation
Your question is mostly a duplicate of this, except that you are looking for the Android version. It is quite similar but here it is, 90º rotation can be obtained by transposing and then flipping the image:
# rotate 90º counter-clockwise
Core.flip(mRgba.t(), mRgba, 0); //mRgba.t() is the transpose
# rotate 90º clockwise
Core.flip(mRgba.t(), mRgba, 1);
For other rotations you can use warpAffine:
Point center = new Point(x,y);
double angle = 90;
double scale = 1.0;
Mat mapMatrix = Imgproc.getRotationMatrix2D(center, angle, scale);
Imgproc.warpAffine(srcMat, dstMat, mapMatrix, Imgproc.INTER_LINEAR);
EDIT - more complete examples follow:
/**
* Image is first resized-to-fit the dst Mat and then rotated.
* mRgba is the source image, mIntermediateMat should have the same type.
*/
private void rotationTutorial(){
double ratio = mRgba.height() / (double) mRgba.width();
int rotatedHeight = mRgba.height();
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);
Imgproc.resize(mRgba, mIntermediateMat, new Size(rotatedHeight, rotatedWidth));
Core.flip(mIntermediateMat.t(), mIntermediateMat, 0);
Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());
mIntermediateMat.copyTo(ROI);
}
/**
* Image is rotated - cropped-to-fit dst Mat.
*
*/
private void rotationAffineTutorial(){
// assuming source image's with and height are a pair value:
int centerX = Math.round(mRgba.width()/2);
int centerY = Math.round(mRgba.height()/2);
Point center = new Point(centerY,centerX);
double angle = 90;
double scale = 1.0;
double ratio = mRgba.height() / (double) mRgba.width();
int rotatedHeight = (int) Math.round(mRgba.height());
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);
Mat mapMatrix = Imgproc.getRotationMatrix2D(center, angle, scale);
Size rotatedSize = new Size(rotatedWidth, rotatedHeight);
mIntermediateMat = new Mat(rotatedSize, mRgba.type());
Imgproc.warpAffine(mRgba, mIntermediateMat, mapMatrix, mIntermediateMat.size(), Imgproc.INTER_LINEAR);
Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());
mIntermediateMat.copyTo(ROI);
}
Note:
These examples might be orientation-specific, I made them for landscape orientation.
You should not call the code from these examples for every video frame. Some of the code should only run once.
If you only need to do 90, 180, or 270 degrees rotation (Which seems to be your case) you better use Core.flip() which is faster.
Here below a method that does it for you:
public static Mat rotate(Mat src, double angle)
{
Mat dst = new Mat();
if(angle == 180 || angle == -180) {
Core.flip(src, dst, -1);
} else if(angle == 90 || angle == -270) {
Core.flip(src.t(), dst, 1);
} else if(angle == 270 || angle == -90) {
Core.flip(src.t(), dst, 0);
}
return dst;
}