Sprite Rotation around a point Andengine/Java - java

Does anyone know how can I get a sprite/bitmap to rotate by a certain angle where the point on one the sides will always remain in the same position.
I have a blue line in the image and I need one of the sides to remain in the same position at all times.
It would be great help if somebody could tell me how to do this.
Im trying to do this in Andengine/Java. So a solution in either would be most helpful.
Thanks
Regards,
Yaro

The AndEngine has a good method called setRotationCenter. :) By default rotation center set to center of sprite (half of width, half of height). You can easily change this point.

The AndEngine is quite limited with regard to rotation and positioning, I've faced a similar problem on my own, and the only way to solve it was overriding methods and extending classes.
I'd recommend you to implantation the a new method which will set the rotation axis, and override the setRotation() method, to use this axis, I guess you could figure out how to rotate the sprite on a different axis by changing its position which changing its rotation

I just find out a solution for rotate a sprite at any center point. First you create a virtual entity and attach your sprite into this entity (notice: position of sprite is relative with your entity), then you move rotation center of the sprite (default at center of image) to overlap the rotation center of the entity. Final, you set rotation for the entity instead of the sprite. Hope this helpful ^^

Related

Why is the rotation point of a sprite using setOrigin is not set to the middle of the sprite?

I wanted to be able to rotate my cursor around my sprite and have it rotate relative to my mouse's position
I have used setOrigin(x,y) to set the pivot point to the center of the sprite
It worked to a degree, but its not exactly what i'm looking for
This was the end result:
https://gfycat.com/gifs/detail/CrazyPleasedIberianlynx
For simplicity and because I don't know what it's officially called, I'll just call the rotation on the center of the sprite the "pseudo" pivot point,
and the point in which the sprite rotates, the one on the grass most of the time, the "true" pivot point
I want to either set the sprite to move the "true" pivot point or to move the "true" pivot point to the sprite
I have looked around for the solution but I couldn't find the answers i'm looking for
Thank you in advance
I think i figured it out.
In regards to the mouseCoordinates, It was part of the problem.
Apparently i needed to unproject the mouse from the camera.
I used gdx.input.getX() and gdx.input.getY() for the projected mouseCoordinates.
As for the matter of centering the "True" pivot point to the sprite, i had to change the spriteCoordinate value of the function i made.
Here is the function in question pastebin.com/06zJ4DtS
copied from the comment i made.

LibGDX coordinates after resizing

I have an actor on the screen, with a DragListener to move him around the stage, and a ClickListener. After a click on it i want to draw a rectangle around him (using ShapeRenderer), the important thing is that the rectangle position is updating after moving the actor. Works good, until I resize the window (I'm using ExtendViewport). The rectangle is not at the correct position (I'm taking the coordinates using Actor's getX() and getY() methods). Tried the Viewport's toScreenCoordinates() method but still it is not correct. Any help with this problem would be very appreciated.
Are you forgetting to update the projection matrix of your ShapeRenderer after resize? Something like this:
viewport.update();
shapeRenderer.setProjectionMatrix(viewport.getCamera().combined);

Rotating Asteroids

I'm trying to make asteroids that spin in my 2D game.
Using the graphics class I am drawing an image I created onto the screen (the asteroid) and moving it across the screen.
My next step was to make it rotate however I am lost at to how to do this.
I was able to make it rotate when I was simply drawing a polygon by changing the vertices of the asteroid however there are no vertices when drawing images, only (x,y) and (length,width).
How can I rotate an image? Is there any inbuilt functionality that does it?
I'm not asking for someone to tell me exactly how to do this, I'm just looking for a push in the right direction as I am a bit lost.
Either use a AffineTransform or use Graphics#rotate. In either case, you should make a copy of the Graphics context first (Graphics#create) first, which will preserve the state of the original context. Just make sure you dispose of the copy when you're done (Graphics#dispose)
Something like this perhaps

Collision Detection With a Gapped Sprite LibGDX

I need an object to be able to go through a gap in a sprite but collide with any part of it. The sprite is similar to that below:
\\\\\\\\\\\\\\\\\\\\\\\\.............///////////////////////////////
The white in the image between the two branches (the dots) is transparent, not white.
I've seen countless tutorials where a rectangle is drawn around the sprite but the problem is that it won't work in this case, as you can probably see. Using area overlap could be a possibility, but I have no idea how to find the area of the sprite, if I could, I could use something like this, as I've seen on another post:
public static boolean testIntersection(Shape shape, Branches branches){
Area shapearea = new Area(shape);
areaA.intersect(new Area(branches));
return !areaA.isEmpty();
}
Spawning them separately isn't an option as the x must be random, but the distance apart must remain the same.
Any help or ideas would be greatly appreciated.

Java - Keeping scaling relative to image's rotation

For an assignment, I need to make a "doll" in Java where you can move it around and rotate the arms and legs to make funny/cool poses. In addition, the legs should be able to stretch (but not get wider, this is important).
The way I wanted to handle the leg stretching was invert the rotation done to the leg to get it back in the neutral straight position, scale it in the y direction, and then re-perform the rotation. However, it appears the scaling is always going to be in the y direction, so once it's rotated, the leg will become wider. It will only look as it should if it's brought back straight and standing.
The code I have right now looks something like this:
leg.transform(AffineTransform.getRotateInstance(legAngle,pivot.getX(),pivot.getY()).createInverse());
leg.transform(AffineTransform.getScaleInstance(1,scaleFactor);
leg.transform(AffineTransform.getRotateInstance(legAngle,pivot.getX(),pivot.getY()));
How can I make sure the scaling stays relative to the image after rotation?
There are a couple of ways that come to mind. The first (simplest, but slowest) method is to render the stretched leg image to an image, then rotate and render that image. Another way is to use Math.cos and Math.sin to apply the proper scaling in each dimension for the desired effect in the image.

Categories

Resources