I have a marching sound file which I play once a specific event occurs, but the issue comes down to having the file fade out in a specified amount of time before being stopped when the volume reaches 0. This project is for Desktop.
Sound marching = Gdx.audio.newSound(Gdx.files.internal("sounds/test.wav"));
The first attempt I had at this was scheduling a task through the Timer:
final long mId = marching.play(1f, 1f, 0);
for (float i = 1; i > 0; i -= 0.01) {
Timer.schedule(new Task() {
#Override
public void run() {
marching.setVolume(mId, i);
}
}, 0.3f);
}
marching.stop();
This, however seemed to invoke marching#stop before marching#setVolume
I decided to take another apporach:
float volume = 1;
final long mId = marching.play(volume, 1f, 0), startTime = TimeUtils .millis();
while (volume > 0) {
if (TimeUtils.millis() - startTime % 300 == 0) marching.setVolume(mId, volume -= 0.1);
}
marching.stop();
I thought that this would reduce the volume based on the amount of time allotted, yet it only made the program freeze.
Is there a straightforward way of fading SFX audio out or some manner of setting frequent sleeps so that I can lower the volume iteratively?
Thanks.
I think the easiest way is to put this in the render method:
float FACTOR = 10; // The bigger the factor, the faster the fade-out will be
float mVolume = 1;
boolean mIsPlaying = true;
public void render(float delta) {
// ...
if (mIsPlaying) {
mVolume -= delta * FACTOR;
if (mVolume > 0) {
marching.setVolume(mId, mVolume);
}
else {
marching.stop(mId);
mIsPlaying = false;
}
}
// ...
}
First solution is wrong, I rewrote it for you:
final long mId = marching.play(1f, 1f, 0);
Timer.schedule(new Task() {
#Override
public void run() {
for (float i = 1; i > 0; i -= 0.01)
{
marching.setVolume(mId, i);
if(i<=0)
marching.stop();
}
}
}, 0.3f);
Second solution freeze your app because you are calling it in the main thread and not in a async task!
However using the render task is not a good idea. You should detach render task from game loop task. Read that: http://gameprogrammingpatterns.com/game-loop.html. Logic of your game should run at the same speed in all machine. Cannot be limited by rendering issue/slow problem.
I find this works much better than using a loop.
I set MUSIC_FADE_STEP to 0.01f (i.e adjust volume by 1% each time through).
I adjust fadeRate (in seconds) as required. 0.01f works well as a starting point.
Timer.schedule(new Timer.Task() {
#Override
public void run() {
if (music.getVolume() >= MUSIC_FADE_STEP)
music.setVolume(music.getVolume()-MUSIC_FADE_STEP);
else {
music.stop();
this.cancel();
}
}
}, 0f, fadeRate);
Related
I am making a game in Android Studio. Now my game is complete, but game speed is different on large displays...
I run my game with this timer:
if(timer == null){
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
if (start_flg) {
handler.post(new Runnable() {
#Override
public void run() {
changePos();
}
});
}
}
}, 0, 20);
}
changePos() looks like this:
public void changePos() {
SPEED_BOX =(screenHeight/280);
long time = System.nanoTime();
double delta_time = (double) ((time - last_time) / 1000000)/10;
last_time = time;
// Move Box
if (action_flg) {
// Touching
boxY += SPEED_BOX*delta_time;
box.setImageDrawable(imageBox1);
} else {
// Releasing
boxY -= SPEED_BOX*delta_time;
box.setImageDrawable(imageBox2);
}
// Check box position.
if (boxY < 0) {
boxY = 0;
box.setImageDrawable(imageBox1);
}
if (frameWidth - boxSize < boxY) {
boxY = frameWidth - boxSize;
box.setImageDrawable(imageBox2);
}
box.setY(boxY);
}
id correct that my deltaTime is allways between 1.5 to 2.9?
but every time I try it in different ways always game speed is not correct.
Is it possible to make my game running same speed on different devices, different screen sizes?
The problem was that screenHeight was screens height in pixels but the game does not use the whole screen. This caused that the speed was different on different devices. So screenHeight should be changed to gamesLayout.getHeight().
I know there are a lot of problems but perhaps by freezing another task to achieve the nearest result possible or by using a parallel thread?
Here is my code:
public void draw(Canvas canvas) {
DrawButtons(canvas);
DrawPercise(canvas);
DrawLines(canvas);
}
private void DrawButtons(Canvas canvas) {
canvas.drawBitmap(Button, 50, 0, null);
}
private void DrawPercise(Canvas canvas) {
if (System.nanoTime() >= AllowedTimeinNano) {
// Save time again for Next if
//if 50000000 nanoseconds passed do it again
AllowedTimeinNano = (long) (System.nanoTime() + (20000000000f / 400));
DoTask();
}
}
private void DrawLines(Canvas canvas) {
for (float i = 40; i < 800; i += 40) {
canvas.drawLine(0, i, 800, i, TablePaint);
}
}
The problem is if my task takes too long or the target device has bad performance, then the timing becomes incorrect, and the whole point of the app is based on this timing. I know this may be impossible but could you give some tips?
I thought I would answer this question to clairify what I said in the comment.
Thread:
public class GameThread extends Thread {
private int FPS = 60;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private Clicker gamePanel;
private boolean running;
public static Canvas canvas;
public GameThread(SurfaceHolder surfaceHolder, Clicker gamePanel)
{
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
#Override
public void run()
{
long startTime;
long timeMillis;
long waitTime;
long totalTime = 0;
int frameCount =0;
long targetTime = 1000/FPS;
while(running) {
startTime = System.nanoTime();
canvas = null;
//try locking the canvas for pixel editing
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.gamePanel.tick();
this.gamePanel.draw(canvas);
}
} catch (Exception e) {
}
finally{
if(canvas!=null)
{
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
timeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime-timeMillis;
try{
sleep(waitTime);
}catch(Exception e){}
totalTime += System.nanoTime()-startTime;
frameCount++;
if(frameCount == FPS)
{
averageFPS = 1000/((totalTime/frameCount)/1000000);
frameCount =0;
totalTime = 0;
System.out.println(averageFPS);
}
}
}
public void setRunning(boolean b)
{
running=b;
}
}
I got it when I took a tutorial when I started up, and I have only used this. Before you can use it there are some things you have to do:
Have a class that extends SurfaceView implements SurfaceHolder.Callback
Replace 'Clicker' with the name of your class.
For FPS calculation:
milliseconds in 1 second / FPS = how often in milliseconds it will update
1000 / 60 = 16.666666
Which means it updates every 0.01666666 seconds
How do I use it?
Simple. (You have to find the propper places to place them yourself)
Creating it:
if(gt == null) {
gt = new GameThread(getHolder(), this);
gt.setRunning(true);
gt.start();
}
Stopping it:
if(gt != null) {
try {
gt.setRunning(false);
gt.join();
gt = null;
} catch (InterruptedException e) {
Toast.makeText(c, "An error occured when stopping the thread.", Toast.LENGTH_LONG)
.show();
}
}
In my experience, 60 FPS is the best FPS to take to secure that this will work on all devices. All though, there are some exceptions.
Normal phones today have 2GB of ram or more. For an instance, S7 has 4 gigabytes of ram. However, if there is 512 MB of ram, I'm not sure as of performance. But there are very few devices left in the world with 512 MB RAM. There are some budget phones, but there are not a lot of those with only 512 MB of RAM unless you would select the really old versions.
So, by using the thread above, you will have a game thread that updates contantly and will never stop. You do not have to worry performance wise, because there are mostly new devices running for real potential customers.
Additionally, this thread works in a very simple way:
FPS is the max FPS and will therefore not go higher, so lower end devices will go for as high as possible up to 60, while newer will stay steady at 60.
All though I have experienced with my phone that it sometimes go up to 62, but it isn't really a problem because it is only two FPS too much.
Remember:
The more actions that happen in methods touched by the gamethread, the more strain it is on the system and on the app. Any game can reach 2000 fps if there is no limit and nothing happens. While loops are fast!
hi am trying to make a small game using canvas and bitmaps i want my game to run the same on all devices i found delta time is the best practice for this but for some reason when i try to implement it into my code i have display issues for example am trying to move my coluds in the sky but when i add the delta they all disapere i dont know if im doing it wrong so please can sombody help me heres the code
private float c1x = 0.0f;
private float c2x = cloudWidth;
private float c3x = cloudWidth * 2;
private float cloudSpeed = 0.1f;
private long curentTime;
private long lastTime = 0;
private double delta;
#Override
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
curentTime = System.nanoTime();
delta = curentTime - lastTime;
lastTime = curentTime;
cloudMovement();
canvas = holder.lockCanvas();
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
holder.unlockCanvasAndPost(canvas);
}
}
private void cloudMovement(){
if(c1x <= 0 - cloudWidth){
c1x = w;
c1y = y.nextInt(rand);
}
if(c2x <= 0 - cloudWidth){
c2x = w;
c2y = y.nextInt(rand);
}
if(c3x <= 0 - cloudWidth){
c3x = w;
c3y = y.nextInt(rand);
}
c1x-=cloudSpeed * delta;
c2x-=cloudSpeed * delta;
c3x-=cloudSpeed * delta;
}
You could use a global FPS mechanism instead which forces a steady FPS on your game :)
If you track the FPS the game will run the same way on any device and you dont need to include delta-times on all update processes.
Here's a code snippet from a FpsTracker i used in an old project:
private static final long SECOND = 1000;
private static final long TARGET_FPS = 40;
private static final long FRAME_PERIOD = SECOND / TARGET_FPS;
private long time = System.currentTimeMillis();
/**
*
* #param startTime
* #return <code>true</code> if the interval between startTime and the time
* when this method was called is smaller or equal to the given
* frame period.
*
* Will return <code>false</code> if the interval was longer.
*/
public boolean doFpsCheck(long startTime) {
if (System.currentTimeMillis() - time >= SECOND) {
time = System.currentTimeMillis();
}
long sleepTime = FRAME_PERIOD
- (System.currentTimeMillis() - startTime);
if (sleepTime >= 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
//TODO handle this properly
e.printStacktrace()
}
return true;
} else {
return false;
}
}
If this method returns false it means that your operations took longer that the timeperiod you gave to one frame. You can react to this by checking the doFpsCheckreturn parameter.
Implementing this in your code would look like this:
#Override
public void run()
{
while(running)
{
if(!holder.getSurface().isValid())
{
continue;
}
startTime = System.currentTimeMillis();
cloudMovement();
canvas = holder.lockCanvas();
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
holder.unlockCanvasAndPost(canvas);
doFpsCheck(startTime);
}
}
By the way - it is good practice to devide your game loop into pro sub processes, one being the update process, the other being the draw process.
For many different Bitmaps you should consider extracting the fields and functionalities into seperate classes containing a draw(Canvas c) and update() method. So you wont get a trillion fields on your main class.
I am writing a game loop, I found the code in the example below here. I have also looked at other ways to do a game loop, such as from this article. I couldn't get any of those ones working though. So I kept with the one from the first link.
What I would like to know:
Is the way I wrote my game loop a good way to do this?
Any suggestions?
Should I be using Thread.sleep(); in my game loop?
Here is my current code:
public void run(){
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
while(true){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double)OPTIMAL_TIME);
lastFpsTime += updateLength;
if(lastFpsTime >= 1000000000){
lastFpsTime = 0;
}
this.updateGame(delta);
this.repaint();
try{
Room.gameTime = (lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000;
System.out.println(Room.gameTime);
Thread.sleep(Room.gameTime);
}catch(Exception e){
}
}
Eventually you'll want to move to something like LWJGL, but let me stress, keep doing what you're doing here for now. It will teach you fundamentals.
Good job on your loop. Looks nice, let me offer a few pointers:
Repaint will not render the screen immediately. It tells the RepaintManager to render when its ready. Use invalidate paintImmediately instead. paintImmediately will block execution until the component has been redrawn so you can measure rendering time.
Thread.sleep typically has a few milliseconds drift. You should be using it to keep your loop from using too much CPU, but make sure you understand if you sleep 10 milliseconds you might sleep 5 milliseconds or you might sleep 20.
Lastly:
double delta = updateLength / ((double)OPTIMAL_TIME);
If updateLength is less than OPTIMAL_TIME, don't call update. In other words, if delta is less than one, don't update. This tutorial explains why better than I ever could.
Overall, it is a good loop, but there are a few missing aspects to what I have found in experience to be the best loop.
You will eventually want to move to LWJGL or some other java game API, but for now, learn the basics of how game-loops work, and what best suits your needs.
Firstly, in answer to one of your points, no. You will do better staying away from
Thread.sleep()
this can stray from the real amount of time you set it to sleep.
e.g. if you set it to sleep for 10 milliseconds, it could sleep the program for 5 to 20 milliseconds.
The second problem I cam immediately see is that you do not have any way to stop the game-loop for a custom stop() method. Try
boolean running = true;
while (running) {
// Your Code Here //
}
Thirdly, you may want to consider changing how you use your delta variable. The way in the code below may be a better use and construction for you.
This is an example of my game-loop that I use in my programs:
#Override
public void run() {
long initialTime = System.nanoTime();
final double timeU = 1000000000 / UPS;
final double timeF = 1000000000 / FPS;
double deltaU = 0, deltaF = 0;
int frames = 0, ticks = 0;
long timer = System.currentTimeMillis();
while (running) {
long currentTime = System.nanoTime();
deltaU += (currentTime - initialTime) / timeU;
deltaF += (currentTime - initialTime) / timeF;
initialTime = currentTime;
if (deltaU >= 1) {
getInput();
update();
ticks++;
deltaU--;
}
if (deltaF >= 1) {
render();
frames++;
deltaF--;
}
if (System.currentTimeMillis() - timer > 1000) {
if (RENDER_TIME) {
System.out.println(String.format("UPS: %s, FPS: %s", ticks, frames));
}
frames = 0;
ticks = 0;
timer += 1000;
}
}
}
the simplest way to refresh repainting could be like this:
public class GameLoop extends JPanel {
private final BufferedImage back_buffer;
bool state = true;
public void init() {
while (state) {
updatePlayer();
delay(5);
}
}
public void delay(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
}
and the repaint function is who update all of the graphics
#Override
public void paint(Graphics g) {
super.paint(g);
grapicDoble.setColor(Color.white);
grapicDoble.fillRect(0, 0, 500, 500);
game.reset();
g.drawImage(back_buffer, 0, 0, this);
}
I try to create a very simple physics engine for my study (processing used for a interactive installation).
The target is to have a ground covered with balls that you can throw around with gestures (based on Kinect information).
Therefor I need to do some basic physic simulation like bouncing and thats what I started with. So there are just balls falling down and bouncing. I simulated the air resistance with a simple 0.995f multiplication on the speed if the ball moves up. Works nice and looks realistic. The main problem is, that the balls never stay calm on the ground. Instead they start to tremble on the ground. That means there is a movement of 1 or 2 pixels up and down.
How can I prevent that without implementing some "borders" on which I set the position directly to the bottom and the speed to zero?
My applet:
public class BubblePhysicApplet extends PApplet {
public static int width = 640;
public static int height = 480;
long lastTime = -1;
Bubble[] mBubbles = new Bubble[10];
Random mRandom = new Random();
public void setup() {
// size(width, height, OPENGL);
size(width, height, P2D);
for (int i = 0; i < mBubbles.length; i++) {
mBubbles[i] = new Bubble(mRandom.nextInt(width), mRandom.nextInt(height), 50);
}
lastTime = System.currentTimeMillis();
}
public void draw() {
background(0);
long tmp = System.currentTimeMillis();
long elapsed = tmp - lastTime;
for (Bubble bubble : mBubbles) {
bubble.animate(elapsed);
bubble.draw(this);
}
lastTime = System.currentTimeMillis();
}
}
The ball/bubble:
public class Bubble {
float mX;
float mY;
float mSize;
float mSpeedX = 0;
float mSpeedY = 0;
public Bubble(int x, int y, int size) {
mX = x;
mY = y;
mSize = size;
}
public void draw(PApplet applet) {
applet.stroke(255);
applet.noFill();
applet.ellipseMode(PApplet.CENTER);
applet.ellipse(mX, mY, mSize, mSize);
}
public void animate(long elapsed) {
updateSpeedY(elapsed);
if (mSpeedX != 0 || mSpeedY != 0) {
checkBorders();
}
}
private void checkBorders() {
if (mY > BubblePhysicApplet.height - mSize / 2) {
mY = 2 * BubblePhysicApplet.height - (mY + mSize);
mSpeedY = -mSpeedY;
}
if (mX > BubblePhysicApplet.width) {
mX = BubblePhysicApplet.width - (mX - BubblePhysicApplet.width);
mSpeedX = -mSpeedX;
}
}
private void updateSpeedX() {
}
private void updateSpeedY(long elapsed) {
mSpeedY += (elapsed / 1000f) * 9.81f;
if (mSpeedY < 0) {
mSpeedY *= 0.95f;
}
mY += mSpeedY;
}
}
It's not only air resistance that slows the ball down, but the fact that it's not perfectly elastic as this line suggests: mSpeedY = -mSpeedY;
The ball absorbs energy when it squishes against the floor before it bounces back, so it doesn't bounce as high. Try a real super ball. I seem to remember it only bounces 80% as high or so. You might try:
mSpeedY = - (0.8 * mSpeedY);
you have to fix your check borders method, read this answer I just gave a complete formulas needed for realistic physical simulation. and it's also more realistic if you move objects using hist method (p = v*dt + 1/2*adtdt)
The problem is that in updateSpeedY we have mSpeedY += (elapsed / 1000f) * 9.81f; even when there is a collision. That said collision is detected later in checkBorders where the speed is flipped mSpeedY = -mSpeedY;. The problem is that if the ball is hitting the floor with a speed near 0, it bounces with a speed of 0 + (elapsed / 1000f) * 9.81f;!!
You have to rethink your code.
in the same fashion you used a friction factor for the air, you can also include a friction factor for the contact with the ground, and which even higher values, so at each contact, it starts to lose eneger rapidly and finally stops