libgdx - find the coordinates different from the point of origin? - android

I 'd like someone to advise me a way to find the coordinates of a point on a sprite in libgdx .
As you can see from the image I have set the sprite with the point of origin on the red dot , and I can not change it.
I would leave the red dot as the source and find the coordinates of the green on the same sprite .
Thanks for your help.
EDIT
#Override
public void create () {
img = new Texture("rocket.png");
font = new BitmapFont();
font.setColor(Color.BLUE);
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
sprite = new Sprite(img);
spacesprite = new Sprite(new Texture(Gdx.files.internal("space.jpg")));
spacesprite.setPosition(0,0);
spacesprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
point = new Sprite(new Texture(Gdx.files.internal("point.png")));
batch = new SpriteBatch();
}
#Override
public void render () {
sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()/2, Gdx.graphics.getHeight() / 2 - sprite.getHeight()/2);
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
point.setPosition(sprite.getX() + sprite.getWidth()/2 - point.getWidth()/2, sprite.getY() + sprite.getHeight()/2);
point.setOrigin(point.getWidth()/2, 0);
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2, Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
if(Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
{
//System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation++);
point.setRotation(rotation++);
System.out.println("Sprite: X" + sprite.getX() + " - Y:" + sprite.getY());
}
else
{
//System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation--);
point.setRotation(rotation--);
System.out.println("Sprite: X" + sprite.getX() + " - Y:" + sprite.getY());
}
}
batch.begin();
spacesprite.draw(batch);
sprite.draw(batch);
point.draw(batch);
batch.end();
}
someone can adapt the code , when I rotate I'd get the position , but they are insecure about my implementation .

if you're looking for a middle point on the rectangular sprite, try something like this:
float x = obj.getOriginX() + obj.getHeight();
float y = obj.getOriginY() + obj.getWidth() / 2;

Either use Gonio, matrix or quaternions to calculate the rotated coordinates.
For a few simple calculations I'd use gonio, something like:
float angle_rad = sprite.getRotation() / 180.0f * PI;
float rotated_x = Math.sin(angle_rad) * y + Math.cos(angle_rad) * x;
float rotated_y = Math.sin(angle_rad) * x + Math.cos(angle_rad) * y;
If you have to do this more often look into matrices or quaternions, matrices are a little easier though: http://en.wikipedia.org/wiki/Rotation_matrix

Related

How to capture detected face image using MLKit firebase FaceDetection

I want to only capture detected face image not whole image on camerasource.
I am drowing box like this
public void draw(Canvas canvas) {
Face face = mFace;
if (face == null) {
return;
}
// Draws a circle at the position of the detected face, with the face's track id below.
float x = translateX(face.getPosition().x + face.getWidth() / 2);
float y = translateY(face.getPosition().y + face.getHeight() / 2);
canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
// Draws a bounding box around the face.
float xOffset = scaleX(face.getWidth() / 2.0f);
float yOffset = scaleY(face.getHeight() / 2.0f);
float left = x - xOffset;
float top = y - yOffset;
float right = x + xOffset;
float bottom = y + yOffset;
this.faceTrackingCords.y = top;
this.faceTrackingCords.width = right;
this.faceTrackingCords.x = left-right;
this.faceTrackingCords.height = bottom;
canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
and trying to capture image like this but cropped image is not as expected.
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
#Override
public void onPictureTaken(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap bitmapCropped = Bitmap.createBitmap(bitmap,(int)faceTrackingCords.x, (int)faceTrackingCords.y,
(int)faceTrackingCords.width, (int)faceTrackingCords.height);
screenImage.setImageBitmap(bitmapCropped);
}
});
Kindly please help

Zoom Imageview using matrix

Hi I am trying to zoom in on a specific point on the image in my imageview with this code:
private void calculateAndZoom() {
matrix.set(getImageMatrix());
printMatrixValues(getImageMatrix());
float startpointY = (start_y_rel/ template_y_rel) * getHeight() * scale;
float startpointX = (start_x_rel / template_x_rel)* getWidth() * scale;
PrintDevMessage.print("Width: " + getWidth() + " Height: " + getHeight());
matrix.setScale(1,1,0,0);
matrix.postScale(scale, scale, startpointX, startpointY);
this.setScaleType(ImageView.ScaleType.MATRIX);
this.setImageMatrix(matrix);
printMatrixValues(getImageMatrix());
}
start_y_rel and start_x_rel is points relative to the template_y_rel and template_x_rel
I am using matrix.setScale(1,1,0,0) to remove any previous zooming and move to the first position.
This code works with scale 3 but when I try another scale it zooms in on the wrong position.
Ok so after 3 days of head-scratching i found the solution:
private void calculateAndZoom() {
float cScale=getMatrixValue(getImageMatrix(),Matrix.MSCALE_X);
float newScale = ((float)1.0/cScale)*scale;
matrix.set(getImageMatrix());
printMatrixValues(matrix);
float startpointY = ((start_y_rel / template_y_rel) * getHeight());
float startpointX = ((start_x_rel / template_x_rel) * getWidth());
PrintDevMessage.print("Width: " + getWidth() + " Height: " + getHeight());
matrix.postScale(newScale, newScale, startpointX, startpointY);
this.setScaleType(ImageView.ScaleType.MATRIX);
this.setImageMatrix(matrix);
printMatrixValues(getImageMatrix());
}
I did not account for the image being larger than the screen and when placed in the imageview the scale is changed. So i had to recalculate the scale i wanted with the old scale like this:
float newScale = ((float)1.0/cScale)*scale;

How to animate a graph being drawn in Android

I need to animate the drawing of a line graph. I'll receive an array of values (probably between 0 and 10) and that has to go on a graph over time. I want the line graph to be drawn, and the drawing should be visible; animated.
I've looked at Anders Ericsson's tutorial here: http://www.jayway.com/2012/08/29/creating-custom-android-views-part-3-animating-your-custom-views-smoothly/#comment-105813
The code here: https://github.com/andersericsson/ViewTutorialPart3
But I can't seem to swing it to work my way.
Is there another solution out there?
The solution I ended up using involved some algebra in the class where I extended View.
The function gets the array of scores (values between 0 and 10) from the activity and uses the values two-by-two as start and end points to a line (note that the end point of one line is the start point of the next line).
It calculates the length of the line and how many segments will be in the line; also how far to move in the x direction and how for to move in the y direction.
Then it calculates the x and y values of the end of the first segment in the line and draws that segment.
Then the xDirection and yDirection is added to the x and y points respectively and the line is drawn again, which now includes the first and second segment of the line. This is done for every segment in the line, and then the final line from point A to point B is drawn.
But it's not complete - the entire for loop in the setData function should be placed in a recursive function, because postInvalidateDelayed() doesn't pause the for loop from executing.
However, nothing is drawn on the canvas at all, hence the link to my other question currently on SO: Why are no lines drawn on my (custom View) canvas?
But for this question, I think that the solution I ended up using is probably not so bad. Comments?
public void setData(float[] scorePoints, float max, int totalScores){
Log.d(TAG, "Get stuff from activity");
scores = scorePoints;
numberOfScores = totalScores;
Log.d(TAG, "Number of scores = " + numberOfScores);
maxScore = max;
Log.d(TAG, "Max score = " + maxScore);
segmentToDraw = (float) 10;
//get the width of the area to draw
width = Math.abs(getWidth() - getPaddingLeft() - getPaddingRight());
//get the height of the area to draw
height = getHeight() - getPaddingTop() - getPaddingBottom();
Log.d(TAG, "Drawable area in view = " + width + " x " + height);
/*
* Now we start filling an array of points.
* The onDraw function will drawLines of groupings of four points in a given array.
*
* For the first segment, we'll draw from the x and y of the first point (which will be in the 1st and 2nd spots in our array)
* to the x and y of the first segment (which will be in the 3rd and 4th spots in our array).
* And then the 3rd and 4th spots in our array will be replaced by a new x and y
* marking the end of the second segment to be drawn from our first point.
*
* This will happen until the x and y is not smaller than the x and y of the final point of the line, any more.
* Then the 3rd and 4th spots in our array will be replaced by the x and y of the final point of the line.
*
* If there are more points to draw, the 5th and 6th spots in our array will also be created and filled with
* the x and y of the final point of the line because it'll be the first two values (x and y) for drawing the next line.
*
* So, yes, there will be duplicates in our array of points to draw, but a grouping of four spots will be used to draw one line,
* and the end point of the first line is the starting point of the second line, so we need it twice.
*/
points.add(getXPos(scores[0]));
points.add(getYPos(scores[0]));
points.add((float) 0);
points.add((float) 0);
x = points.get(0);
y = points.get(1);
startPoint = scores[0];
endPoint = scores[1];
for(int i=0; i<scores.length-1; i++){
String thePoints = "";
if(i>0){
startPoint = scores[i];
endPoint = scores[i+1];
x = points.get(i*4);
y = points.get((i*4) + 1);
}
startPointX = getXPos(startPoint);
startPointY = getYPos(startPoint);
endPointX = getXPos(endPoint);
endPointY = getYPos(endPoint);
distanceOfLine = (float) Math.sqrt(Math.pow((endPointX - startPointX), 2) + Math.pow((endPointY - startPointY), 2));
Log.d(TAG, "Distance of line = " + distanceOfLine);
//get number of segments in line
numberOfSegmentsInLine = (int) (distanceOfLine/segmentToDraw);
Log.d(TAG, "Number of segments in line = " + numberOfSegmentsInLine);
//get distance to move in Y direction
yDirection = (float) ((endPointY - startPointY)/ (float) numberOfSegmentsInLine);
Log.d(TAG, "Move " + yDirection + " in Y direction");
//get distance to move in X direction
xDirection = (float) ((endPointX - startPointX)/ (float) numberOfSegmentsInLine);
Log.d(TAG, "Move " + xDirection + " in X direction");
//loop for each segment
for(int j=0; j<numberOfSegmentsInLine; j++){
x += xDirection;
y += yDirection;
points.set(points.size()-2, Float.valueOf(x));
points.set(points.size()-1, Float.valueOf(y));
Log.d(TAG, "Line : " + (i+1) + " Segment : " + j);
Log.d(TAG, "X = "+ (x+xDirection) + " Y = " + (y+yDirection));
Log.d(TAG, "Call invalidate now!");
//invalidate();
//postInvalidateDelayed(delayMilliseconds);
}
//draw final line
points.set(points.size()-2, endPointX);
points.set(points.size()-1, endPointY);
invalidate();
//postInvalidateDelayed(delayMilliseconds);
if(i<scores.length-2){
points.add(endPointX);
points.add(endPointY);
points.add((float) 0);
points.add((float) 0);
}
for(int k =0; k<points.size(); k++){
thePoints = thePoints + " : " + points.get(k);
}
Log.d(TAG, "Points array = " + thePoints);
}
}
#Override
public void onDraw(Canvas canvas){
//setWillNotDraw(true);
Log.d(TAG, "DRAW DAMNIT!!!");
Log.d(TAG, "Width = " + (int) width + " Height = " + (int)height);
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
paint.setColor(Color.RED);
//paint.setAntiAlias(true);
//paint.setShadowLayer(4, 2, 2, 0x81000000);
Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
String drawPointsGo = "";
float[] drawPoints = new float[points.size()];
for(int i=0; i<points.size(); i++){
Float f = points.get(i);
drawPoints[i] = (float) (f != null ? f : 0.0);
drawPointsGo = drawPointsGo + " : " + drawPoints[i];
}
Log.d(TAG, "Draw Points: " + drawPointsGo);
canvas.drawLines(drawPoints, paint);
}
Try looking into CountDownTimer or Runnable so that onTick() or when it's time to post you can update your line by drawing a few more pixels. This would let you draw one line after the other but would give the effect of animation.
Here is the general direction to try:
Make sure you are comfortable with Canvas drawing.
Then you will want to create a new View (subclass it) and override the onDraw method.
This method should only draw the graph partially, based on the elapsed time since the start of the animation.

Is there a method that checks if an libgdx Scene2d Image or Actor is touched?

here's my sample from the libgdx android game that I want to create. Nothing special yet, cause I'm just starting my adventure with Android games.
Here is a couple of questions I would like to get answered
So far I am using the Gdx.input.isTouched function and check if that matches the bunny coordinates, I do that for each bunny and at the end I would like to have 11 of them and that's just too much to write. Is there a way to check if the Image class from import com.badlogic.gdx.scenes.scene2d.ui.Image; was touched? Or else does the Actor class has that feature?
Another thing is that when I instantiate the bunny class and I click the bunny, all of them are changing to "scared". It's because the Scene2d.Image texture has to be static. Is there a way to change that?
if(Gdx.input.isTouched()){
x = Gdx.input.getX();
y = Gdx.graphics.getHeight() - Gdx.input.getY();
// **** Show Coordinates **** \\
if (x < 420)
font.draw(batch, "x:" + x +
"\n y: " + y, x, y);
else
font.draw(batch, "x:" + x +
" y: " + y, x-65, y);
// **** End Show Coordinates **** \\
//if krolik (bunny) is touched add highscore and change texture to scared
if (x >= krolik.pos.x && y >= krolik.pos.y
&& x <= krolik.pos.x + 64 && y <= krolik.pos.y + 64)
{
krolik.scared();
highscore+=100;
}
else if (x >= krolik2.pos.x && y >= krolik2.pos.y
&& x <= krolik2.pos.x + 64 && y <= krolik2.pos.y + 64)
{
krolik2.scared();
highscore+=100;
}
}
else{
krolik.normal();
}
// **** Show Highscore **** \\
font.draw(batch, "Highscore: " + highscore, 350, 300);
batch.draw(krolik.getTexture(), krolik.pos.x, krolik.pos.y);
batch.draw(krolik2.getTexture(), krolik2.pos.x, krolik2.pos.y);
batch.end();
Why is it that you are using Image but not drawing with Stage? Image and other Actors are for using Stage which can take care of your touch events and drawing very easily.
Create them like this:
for (int i = 0; i < KROLIKS_COUNT; i++) {
final Image krolik = new Image(nonScaredTextureRegion);
krolik.setClickListener(new ClickListener() {
#Override
public void click(Actor actor, float x, float y) {
krolik.setRegion(scaredTextureRegion);
highscore+=100;
}
});
krolik.x = startPosX;
krolik.y = startPosY;
stage.addActor(krolik);
}
set the stage as the input processor:
Gdx.input.setInputProcessor(stage);
then in render, simply:
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();

Retrieve the X & Y coordinates of a button in android?

I've been working on Android for a while and would like to know if it is possible to retrieve the position of a button in android.
My target is to get the X & Y coordinates and print them on the LOGCAT.
Some example to show me how would be appreciated.
Thanks
Sure, you can get these, make sure the views are drawn atleast once before you try to get the positions. You could try to get the positions in onResume() and try these functions
view.getLocationInWindow()
or
view.getLocationOnScreen()
or if you need something relative to the parent, use
view.getLeft(), view.getTop()
Links to API definitions:
getLocationInWindow
getLocationOnScreen
getLeft
getTop
Like Azlam said you can use View.getLocationInWindow() to get the coordinates x,y.
Here is an example:
Button button = (Button) findViewById(R.id.yourButtonId);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
Bonus - To get the center point of the given view:
Point centerPoint = getCenterPointOfView(button);
Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")");
private Point getCenterPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] + view.getWidth() / 2;
int y = location[1] + view.getHeight() / 2;
return new Point(x, y);
}
I hope the example can still be useful to someone.
buttonObj.getX();
buttonObj.getY();

Categories

Resources