I am using png's in my tower defense game. How would I rotate a picture so it follows the movement of another picture (like a turret always pointing at a target)?
Look into the AffineTransform class. The rotate method is what you're looking for. Here's an example!
If you've got some other specific game-development-related questions, there's a Game Development StackExchange site too.
Related
I want to turn my player in the direction he is looking at. The whole is so similar to the behavior of the Bird in FlappyBird. He always looks in the direction in which he moves. I am programming this in Android Studio with libGDX and I have a class in which the game is played and one in which I have information about my player.
I have unfortunately no code example, because everything I have, would explain nothing and would make the situation only more complicated.
Please write a comment if you are confused.
Thanks for any help! :)
I think what you are looking for is a way to flip the sprite. Sadly, I don't think libgdx supports this. But what you could do is draw 2 images of your sprite(one in each direction) or use some drawing program to flip your current image. Then in your sprite class, in your update or render method you would change the sprite image to the corresponding direction using sprite.set(new Texture())
I'm quite new to LibGDX and OpenGL, but I managed to make a simple liquid simulation using Box2D API. See this link (this is someone's else animation):
Physics Liquid
Currently I render the liquid particles as circles just like in the first image, but I want to make it look more natural like on the third one.
The answer might be to use distance field and I tried this approach, but with no effect. I'm drawing each particle as a texture using SpriteBatch class, but that can be changed. I made a texture (from a procedural Pixmap) that represents each particle as a filled circle, with alpha channel decreasing further from the center, so the effect is similar to the second picture.
Now, I must enable a threshold filter to alpha channel, something like: "draw only pixels with alpha > 0.5". This is post-processing step, because it matters what is the alpha channel of a pixel after all particles have been draw. Might or might not be done with shaders (ProgramShader), but after some research I still have no clue how to do this. Thanks for ANY help.
EDIT: this example explains the method, but it's implemented in ActionScript.
This can easily be done using shaders, but funny thing is that you don't need to write them.
"draw only pixels with alpha > 0.5" is also used while rendering distance field fonts (fonts which look good even when scaled up).
Follow this link : https://github.com/libgdx/libgdx/wiki/Distance-field-fonts
Directly jump to the last step.
You should find exactly what you want.
Hope this helps.
In a game that I am creating at the moment I want to make it so that the player holds a gun.
Right now I have the gun and the player as one image and it works but it would be alot better to have the gun and the player as seperate images because right now it looks like the player shots bullet from it's forehead and if I would have the gun as an seperate object it would be easier to make the bullets shot out of the gun. I will also implement a weapon switching system later on so then it will also be easier to have the guns as seperate objects.
The problem is that because I have used an AffineTransform and Vector2d to rotate the player to always face the mouse cursor I can't manage to make it so that it always looks like the player holds the gun. It's more that sometimes the gun is a little inside the player, sometimes it looks fine and sometimes the gun is floating in the air, either infront of the player or at the side of the player.
Is there any easy way that I could make the gun "stick" to a part of the player object?
You probably "just" need to use the right anchor, for example in rotate. To make this easier, imagine that the Gun bitmap (or Shape?) has the same size as the player.
I tried to illustrate that in this drawing:
The left image is the player, and the right image is the gun. Notice that the gun image has the same dimension as the player image, but is of course almost empty. The red point would be the anchor of the rotation - for example in the center of the image, even though that doesn't matter. If you superimpose the gun on the player and rotate both using the same anchor the gun will always be in the right place.
If you know your linear algebra you can of course also solve this analytically. But I would suggest first getting it to work the way you want, and then exploring more elegant solutions from there. A good quick read I found is this four part blog post. Part 3 and 4 touches on your question. If you want a more thorough introduction any linear algebra textbook will do, or you might like this free online course.
In our game we make the sword and shield stick to the appropiate position of the sprite of the player/enemy by having helpermethods like the ones below:
public int getPlayerLeftHand()
public int getPlayerRightHand()
And everytime the player is drawn (in our game he always has a shield and sword) his sword/shield is drawn at his right/left hand respectively by calling the above methods. The implementors (player/enemy) decide where their hands are so you don't have to worry at all about whether they are rotated or not, you know that every time you call the method the implementors take care of giving you the correct coordinates, in our case by checking what direction they are facing then displacing the coordinates appropriately.
Alternatively if you want a more OOP approach you could pass the player/enemy to the weapon and let them decide how and where they want to be rendered.
You solve this by accumulating transformation matrices.
First, you have a transformation matrix that positions the player. Next, you define a matrix that positions the gun in the hand of the untransformed player. Multiply the two matrices together, and you have one that positions the gun in the hand of the transformed player.
Finally, while you're at it, write a transform matrix that positions a bullet relative to the barrel of the gun. Multiply all three matrices together, and your bullet is where it belongs.
Your 3d graphics library will have routines to do all this for you.
I have to be blunt though: if you don't already know all this, you may have bitten off more than you can chew.
I want to implement a TPP Camera for my project, but something is not working + i don't know if i use the right concept.
Should I rotate the whole scene model-view matrices except my main model, which will be centered on the screen or rotate the lookAt camera?
Other thing is how to make the model move in given direction after rotating? (I think moving the whole scene makes it easier?) + how to add collision detection to it?
Collision detection is nothing to do with openGL you use you game state variables to work that out you can manage it in the same loop as the game where you do user input and display.
You should use a LookAtMatrix for the third person camera you will have the eye component behind the player and the at somewhere infront. Persective can be implemented by using a perspective matrix.
So the matrix multiplication will look like.
PerspectiveMatrix * LookAtMatrix * worldSpacePosition
Here is a good answer from gamedev explaining a lookatmatrix, most OpenGL / Computer Graphics books will also cover this.
Are you working with the new or old pipeline model?
I am getting started on a game in JAVA, which eventually shoould become an android app. anyway, i was wondering How to rotate an image (I eventually want to rotate a character to face the mouse, if you know how to do this it would be AMAZING!). Anyway, this is what i use to load the image:
ImageIcon i = new ImageIcon("C:/image.jpg");
Any ideas? a rotate(Image image, int angle){} method would be great!
If you knew how to rotate an image towards the mouse, that would be even better! Thanks for your time
you can't make a game with ImageIcons. you have to use gava.awt.Paint2D and paint to an image. then you can rotate it by setting the affline transform. here i answered a similar wuestion in more depth:
Platform Game - Getting the Object to adjust angle according to mouse position
and a tutorial on java game programming in general:here
Take a look at this tutorial illustrate how to Rotate an image using Java 2D AffineTransform class .