My sprite rotation doesn' t stay in place - java

So I want my sprite to rotate at my players position, it rotates, but appears some place to the left of the player. I cannot find anything on the internet that helped me with this.
Here's my code
at.setToRotation(Math.PI / 2, Main.p.x, Main.p.y / 2);
if (!rotate) {
g.setTransform(at);
rotate = true;
}
g.drawImage(item0, Main.p.x + 1, Main.p.y - 15, null);
Yes I know it's not the best code, I'm still a beginner, don't be too harsh please.

drawImage might use the sprites center to set it. Try adding half of the width of the sprite to x.
EDIT
Or maybe the setToRotation function uses the center point of the picture.

Related

How To Draw A Flower In Android Each Petal By Petal

In android I would like to draw flower by adding each petal from a center point. I used setRotation on the image view but the center point is different for each petal. (I mean the center point from the flower) Can anybody look at my code and suggest me correction? Thanks.
int angle=0;
int ypos=500;
int xpos=500;
RelativeLayout layout = (RelativeLayout)findViewById(R.id.ln1);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(150,400));
image.setX(xpos);
image.setY(ypos);
image.setPadding(-7,-30,-10,0);
image.setPivotX(1.0f);
image.setScaleX(1.5f);
image.setScaleY(1.5f);
image.setImageResource(R.drawable.petal);
image.setRotation(image.getRotation() + angle);
angle=angle+36;
layout.addView(image);
}
Image I get is this
When you rotate the image, the rotation is done with the top left corner of the image, not the center of the rotated image.
Below image might illustrate this. The black square represents your image. The lefthand site shows the situation you have now. The righthand side shows the situation you want.
Before rotating, you should subtract half the width from the x position, and add half the height from the y position. Then you should get the desired image.
As user Ralf Renz pointed out in their comment, you could also simply start with an angle of -36. This is a useful workaround.
I accomplished this using the following strategy.
image.setPivotY(-1);
Now the flower was coming in a different design because petal was Tilted. So I Tilted my actual image to the opposite direction using inkscape and now I get the desired output.

LibGDX down is up and up is down Gdx.input.getY()

I'm sure this is something very simple. I am having a major brain freeze. I have been inverting it, subtracting from it, multiplying to it, adding to it. No matter what up is down and down is up.
if (Gdx.input.isButtonPressed(Buttons.LEFT)) s.drawCirc(c, Gdx.input.getX(), Gdx.input.getY(), 100, 100);
The x axis is fine the culprit is the Gdx.input.getY(). When I move my finger up it goes down and down it goes up.
My circle lines up in the middle but when I go up it goes down and when I go down it goes up. I would like to understand what exactly is going on here and why it is happening.(& of course how to fix it please)
In case you need to know c is my camera and here is the code in case this is the culprit
c = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
and in my drawCirc method I am using
shapeRenderer.setProjectionMatrix(c.combined);
This is because the coordinate system used for input and the coordinate system used by the camera are opposite of each other on the y axis. Ideally when getting inputs, you want to unproject the input to the camera so that all of the coordinates are nicely aligned. You can do this using c.unproject(input). Here's a code example:
if (Gdx.input.isButtonPressed(Buttons.LEFT))
{
Vector3 input = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
cam.unproject(input);
//Now you can use input.x and input.y, as opposed to Gdx.input.getX() and
//Gdx.input.getY(), to draw the circle
s.drawCirc(c, input.x, input.y, 100, 100);
}
LibGDX uses OpenGL which uses another coordinate system.
It uses left to right, bottom to top.
So in OpenGL 0,0 means bottom left corner.
Use
c = new OrthographicCamera();
c.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
First parameter means Y-Down
More information here: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html
Check this link for more information about using LibGDX with down-y.
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/YDownTest.java

How can I tile an image in X direction while it is scrolling at a velocity?

Hey all i've been scowering the internet for days now, ive also been reading up on this etc and i am in desperate need of help.
I am writing a 2D game and I need the background to move and my character will be stationary and he'll jump over stuff.
If anyone can push me in the right direction or even provide a snippet of code i would be more than greatful.
I think the easiest way to do this is to draw the background image twice as your character scrolls with something like this, but I'm not sure how to do it with HorizontalScrollView.
//x , y, width, height
Background b1 = new Background(0, screenHeight/2, screenWidth, screenHeight);
Background b2 = new Background(screenWidth, screenHeight/2, screenWidth, screenHeight);
//update loop
b1.x-=speed;
b2.x-=speed;
if(b1.x+b1.width/2<0){
b1.x=b2.x+b2.width/2+b1.width/2;
}
if(b2.x+b2.width/2<0){
b2.x=b1.x+b1.width/2+b2.width/2;
}
You could use this sort of method with multiple types of background images that you update at different speeds.
For anyone who is still struggling with this I worked it out:
Download corona SDK and program with that its so much easier:
function scrollBackground(self,event)
if self.x < [Enter Background width in pixels] then
self.x = [Enter Background width in pixels]
else
self.x = self.x - [Speed - low number like 5 is slow scrolling, high number like 18 is fast scrolling]
end
end
Use the above function to for the scrolling background then add this underneath:
function scene:enterScene(event)
background.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background)

Zooming in Slick2D

I'm working on a really low res game that I need to zoom in to make it visible. I know I can use Graphics.scale(float x, float y) but I'd like to zoom into the center. How can I scale the Graphics in the center? Is there an easier way to do low res games?
I think you could translate(float x, float y) your drawing surface (so the origin (0, 0) is in the center) and then zoom in. Then you can use resetTransform() to remove the effect.
If that doesn't work, just move your upper/left rendering offset while you're zooming in via experimentation until you get it right. Once you get it figured out, put that logic into a method called zoomOverPoint(float x, float y) and then you'll be set.
It's a fairly crude solution, but what if you just make everything bigger and move it?
For example, let's say you had a square and a circle,
You want to zoom in on the square which is at coordinates 200, 100
The circle is at coordinates 500, 400
So to zoom in on the square you move it towards the center of the screen and increase the size of it gradually as you zoom in, at the same time, the circle will also be moving off the screen and getting relatively bigger. I don't know if that makes any sense, but you'd probably have to figure out some complicated VecMath for that.

JAVA: 2D Game Adventure. Collision Detection problem

This is about animation in JAVA. I had success when using same dimension on all picture. But if i keep all picture dimension on same size(width and height) i get some bug which when player punch. Before player's hand touch enemy body, enemy died
But others with my case where idle, run, and punch has an different
dimension. Punching animation facing to the left became very strange.
Should his hand hit to the left but his body shifts to the right. This
is because I draw on the x & y are the same.
How can I fix it? Need instructions :D
I use png coz support transparent
I think this can fix with 2 option
1. Fix my collision detection
2. Fix of drawing position my image when some condition occur
Trying to picture your problem, hope this helps.
I am typeing directly from my head, so there are might errors in code
fixing coalision decection
i would try this
Image fist
Image enemy
//in paint
g2D.drawImage(fist,x,y,this);
g2D.drawImage(enemy,x1,y1,this);
Rectangle2D myFist = new Rectangle2D.Double(x,y,fist.getWidth(this),fist.getHeight(this));
Rectangle2D myEnemy = new Rectangle2D.Double(x1,y1,enemy.getWidth(this),enemy.getHeight(this));
if (myEnemy.contains(myFist){
//action u want to happend
}
I think something like this should fix coalision problems
I am seeing this as mario a game on sega
Fix of drawing position
//arm image can be the same image if u want
Image leftArm;
Image rightArm;
image headLegsAndTorsoLeft;
image headLegsAndTorsoRight;
//where am i looking in game if true i look to the leftside of user thats playing
boolean turnedLeft
//in paint
if(turnedLeft){
//this lets it look like he is turned to the left with his right arm in the visible behind his left.
//draw right arm
g2D.drawImage(rightArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoLeft,x-3,y,this);
// draw left arm a bit more in x coor
g2D.drawImage(leftArm,x-6,y,this);
}else{
//this lets it look like he is turned to the right with his left arm in the visible behind his right.
// draw left arm
g2D.drawImage(leftArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoRight,x-3,y,this);
//draw right arm a bit more in x coor
g2D.drawImage(rightArm,x-6,y,this);
}
same order for animation of arms, ultimatly i would use different methods animations for torso, leftarm, rightarm
something like keypressed leftarrow torso does walking animation left, hit left arm key moves left arm,hit right arm key moves right arm,thats 3 for lets say left arm, now u need another 3 for when ur char is moved to the right.
Thats how i would try to do things.

Categories