libGDX: Let an object rotate in the direction in which it moves - java

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())

Related

LibGDX make the world move and the player not

I'm working on a simple game with libGDX and want to make the main character to be fixed in the center of the screen and the world move when I press a button. I was wondering how to do... I was thinking to add a physicsBody to the world that contains other bodies and apply impulses to it when the button is pressed, is this possible in libGDX? And if it is, can i apply other impulses or forces to the bodies contained in the world's physicsBody? I think this way would be the best for me if it is possible, because i have to work a lot with physics, but if you have other ideas tell me please
There's no need to think about applying forces to all the non-character objects, that's just going to get messy very quickly.
The simple solution is to move your camera so that it always looks at your character. So your game loop may look something like:-
Process input
Update physics, character and other entity positions.
Move camera to point at character's new position.
Render
This way, you can update your game world without having to think about the camera at all. Then, when it comes to rendering, you can position your camera and render your graphics without needing to know anything about the game physics. It keeps the physics and rendering relatively independent, and makes it much easier to change things in the future.
For example, you may later decide that you want the camera to follow your character for the most part, but then follow a baddy whilst it is their turn. This is now easy to do, you just specify the character / entity to look at in your game logic, and then position the camera to look at whatever target that is, before you render.

Sidescroller game with libgdx / Box2D with moving floor instead of moving player

I'd like to write a sidescroller game with libgdx and Box2D.
But instead of the moving the player and the camera to the right, the player should stay at his position and the floor should move to the left. Crates should be placed randomly and move from the right to the left of the screen as well. In addition the player should also be able to move to the left and the right of the screen without the game stopping to scroll the level.
I have no problems with using libgdx or Box2d but I'm not sure what is the best way to achieve my goal. I'd like to use physics because I will also have some bouncing balls etc. in the game which should show a physically correct behaviour and should interact with the player.
I have some ideas how to solve my problem:
Apply constant force or velocity to the floor and the crates which pushes them to the left and apply a counter force to the player so it stays at his position. When pushing the left or right button to move the player the counter force is slighty enhanced or decreased.
(As physic simulation is not 100% percent accurate I'd like to avoid this)
Move the position of the floor, player and crates but this would subvert the whole physics thing.
Use physics for anything but the player and move him directly. Therefor I'd have to do any collission detection by myself
Unforunately I'm not happy with any of these solutions. Has anybody faced a similar problem or has any advice how to solve this problem in an elegant way?
Many thanks in advance.
I recommend you to create two separate cameras: for player and for ground(floor).
Such way you will be able to move your "ground camera" as you want, the player will stay on his position. Then just don't move your player camera, move only the player body ,such way you will get effect of racetrack.
I use 4 cameras on my game (for ground, for player, for HUD and for background) it provides you lots of fixability and you can create cool scrolling effects.

collision & touch + shoot methods for Android

I really need help. I am making a game app for my final year project. It is a simple game where you have to shoot a ball into a target by rebounding of walls or angled blocks. However i need help in 2 areas:
the shooting mechanism is similar to that of stupid zombies. There is a crosshairs where you touch on the screen to indicate which direction you want the ball to be shot at. On release the ball should move into that direction and hopefully gets into the target and if not gravity and friction causes it to come to a stop.
The problem is how do I code something like this?
I need the ball to rebound of the walls and I will have some blocks angled so that the ball has to hit a certain part to get to the target. The ball will eventually come to a stop if the target is not reached.
How can I make a method to create the collisions of the wall and blocks?
I have spent the last weeks trying to find tutorials to help me make the game but have not found much specific to the type of game I am making. It would be great if sample code or template could be provided as this is my first android app and it is for my final year project and i do not have much time left.
Thank you in advance
akkki
Your question is too generic for stack overflow no one is going to do your project for you. Assuming you have basic programming experience if not get books and learn that first.
Assuming you already chose Android because of your tag, and assuming 2d game as it is easier.
Pre requests:
Install java+eclipse+android sdk if you havent already.
Create a new project and use the lunar landar example, make sure it runs on your phone or emulator.
Starting sample:
The lunar landar has a game loop a seperate thread which constantly redraws the whole screen, it does this by constantly calling the doDraw function. You are then supposed to use the canvas to draw lines, circles, boxes, colours and bitmaps to resemble your game. (canvas.draw....) Lunar landar does not use openGL so its slower but much easier to use.
Stripping the sample:
You probably don't want keyevents or the lunar spaceship!
Delete everything in the onDraw function
Delete the onKeyUp, onKeyDown
Delete any errors what happen
Create a new
#Override
public boolean onTouchEvent(MotionEvent event){
return false;
}
Run it you should get a blank screen, this is your canvas to start making your game... You mentioned balls, break it down to what a ball is: A position and direction, create variables for the balls x,y direction_x and direction_y the touch event will want to change the balls direction, the draw event will want to move the ball (adding the direction x,y to the ball x,y) and draw the ball (canvas.drawCircle(x,y,radius,new Paint())) want more balls search and read about arrays. Most importantly start simple and experiment.
2 collisions
Collisions can be done in the dodraw function and broken down to: moving an object, checking if that object has passed where it is supposed to go and if so move it back before anyone notices.... There are many differently techniques of collision detection:
If your walls are all horizontal and vertical (easiest) then box collisions checks the balls new x,y+-radius against a walls x,y,width and height its one big if statement and google has billions of examples.
If your walls are angled then your need line collision detection, you basically have a line (vector) of where your ball is heading a vector of your wall create a function to check where two lines collide and check if that point is both on the wall and within the radius of your ball (google line intersection functions)
or you can use colour picking, you draw the scene knowing all your walls are red for example, then check if the dot where the new ball x,y is, is red and know you hit
Good luck, hope this helped a little, keep it simple and trial and error hopefully this gets you started and your next questions can be more specific.

How do i rotate an image in JAVA?

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 .

Game Development, strange image effect while moving camera

I'm developing Side Scroll 2D Game, using AndEngine
I'm using their SVG extension (I'm using vector graphic)
But I discovered strange and ugly effect, while moving my player (while camera is chasing player exactly, means while camera is changing its position)
Images of my sprites looks just different, they are like blurred or there is effect like those images would be moving (not changing their possition, just jittery effect, really hard to explain and call this effect properly) Hopefully this image may explain it a bit:
Its more or less, how does it look in the game, where:
a) "FIRST" image is showing square, while player is moving (CAMERA isn't) image looks as it should
b) "SECOND" the same image, but with this strange effect "which looks like image moving/blurring during camera moving [chasing player])
Friend of mine told me that it might be hardware problem:
"the blurring that you notice is actually a hardware problem. Some phones "smooth" the content on the screen to give a nicer feel to applications. I don't know if it's the screen or the graphics processor, but it doesn't occur on my wife's Samsung Captivate. It happens on my Atrix and Xoom though. It's really noticable on the Xoom due to the large screen size."
But seems there is way to prevent it, since I have tested many similar games, where camera is chasing player, and I could not notice such effect.
Is there a way to turn this off in code?
I'm grateful for previous answers, unfortunately, still problem exist.
Till now, I have tried:
casting (int) on setCenter method which is being executed on updateChaseEntity
testing game using PNG images, instead of SVG extension and vector graphic
different TextureOptions
hardwareAcceleration
If someone have different idea, what may cause this strange effect, I would be really grateful for help - thank you.
Some devices (Xperia Play) bleed everywhere when trying to draw things that are moving quickly. For example a red icon on the application list leaves a blur behind it. You could try hardwareAcceleration in the manifest (on and off) to see if it makes a difference.
You'd probably get the same effect if you weren't using svg
When your player's just going to the right and camera begins chasing him, all other sprites except player are moving to the left. Try to print the absolute coordinates of your "blurring" sprite (or some of its anchor points) to the log. The X-coord of sprite should be decreasing linearly. If you notice it's increasing some times, it could be a reason of blur.
Hope this will help.
It sounds like it's due to the camera moving in real increments, making the SVG components rest on non-integer bounds, and the SVG renderer making anti-aliasing come into effect to demonstrate this. Try moving the camera in integer increments by casting camera values to int.
I'm not familiar with this engine but I wonder why would you use vector graphics for pixelated art style. I'll be surprised if your character in the screenshot is really a vector art... maybe it's texture imported in SVG? I attempted back in the day to use flash a few times and I was making the same mistake... I'm not saying it's not possible but it's not intended to create pixel art with flash or any other vector software. There is a reason why most flash games have similar look.
Best way to debug it, is try a different looking sprite.
Maybe it is just the slow response time of your device display.
I'm also an Andengine developer, and never seen such behavior.
Sometimes you fix jittering using FixedStepEngine, it might help.
If you can post your code maybe we can better help you.

Categories

Resources