Search code examples
javaandroidtimergame-physicsmathematical-optimization

Calculating time of simulation in Java


I am doing a project on a bird killing game. Every thing working fine but i want that my bird cross the screen in 35 seconds for any given mobile screen. And after every 20 seconds its time reduced to 31. What will be the mathematical formula(for speed) to cross the screen in 35 seconds? Currently am updating x-axis value in update method and creating rectangle for the bird-sprites.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.Random;
public class Birds extends GameObject implements View.OnTouchListener{
private Bitmap spritesheet;
private Rect rect;
public boolean firstTym = true;
private Animation animation = new Animation();
private String tag = "";
private int  y,touchX,touchY;
public int x=0;
private long startTime;

public Birds(String tag)
{
    this.tag = tag;
    spritesheet = BitmapFactory.decodeResource(Constants.res, R.drawable.bird_sprites);
    dy = 0;
    if(Constants.Width > 1300) {
        width = 120;
        height = 140;

    }
    else {

        width = 80;
        height = 72;
    }
    Bitmap[] image = new Bitmap[5];


    for (int i = 0; i < image.length; i++)
    {
        image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
    }

    animation.setFrames(image);
    animation.setDelay(10);
    startTime = System.nanoTime();
}
public void update()
{
    if(!firstTym) {
// here i am updating speed of bird in x-axis.
//i want bird to cross the screen in 35 seconds
        x += Constants.speed;
        Log.e("speed = ","" + Constants.speed);
    }
    else
    {
        Random r = new Random();
        r.nextInt(Constants.Width);
    }

    if(x > GamePanel.WIDTH){
        Constants.missed ++;
        x = 0;
    }
    if(y > GamePanel.HEIGHT)
    {
        x = 0;
    }
}
public void draw(Canvas canvas) {
    Random r = new Random();
    if (x == 0 || firstTym) {
        y = r.nextInt(GamePanel.HEIGHT - 150);

        Constants.RAND = r.nextInt(1);
        firstTym = false;
    }
    animation.update();

    y += Constants.RAND;
    rect = new Rect(x, y, x + 80, 72 + y);
    setRect(rect);
    setTag(tag);
    canvas.drawBitmap(animation.getImage(), null, rect, null);
    if (x < 0) {
        canvas.drawBitmap(animation.getImage(), null, rect, null);
    }
}
public int getX()
{
    return x;
}
public int getY()
{
    return y;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    touchX = (int)event.getX();
    touchY = (int)event.getY();
    return true;
}
}

Solution

  • Speed is distance over time. If I move 10 meters in 2 seconds then I am moving at 10 divide by 2 meters per second which is 5 meters per second. We can write it as s = D / t where s is speed, D is distance, and t is time. With that we can arrange it so that we can define any of the components in terms of the other two.

    thus we get

    s = D / t; // get the speed in term of distance and time
    t = D / s; // get the time in term of distance and speed
    D = t * s; // get the distance in term of time and speed
    

    You have the distance D in pixels (ie the width) and the time, in seconds. You will need to get the system clock time startTime when you start the bird flying, the current time timeNow. You will need the x and y position where the bird starts, and the x and y position xEnd,yEndof where the bird will be in the time, and the time you want it to take time

    At the start you need to set up the variables you need. I Don't do java (yuck) so can not remember the class to get system time. Just look it up in the reference docs.

    // set up
    time = 35; // how long to cross the screen
    x = 0; // start pos
    y = 100; // 
    xEnd = screenWidth;  // end pos
    yEnd = 100;
    startTime = ?.now() // ? is whatever class you get the system time from 
    

    With all that to get the position of the bird.

    // each frame get the time in seconds
    timeNow = ?;
    if( timeNow - startTime < time) { // make sure time is not over
        currentX = x + ((xEnd - x) / time) * (timeNow - startTime))
        currentY = y + ((yEnd - y) / time) * (timeNow - startTime))
    }else{
       // All done
    }
    

    You know its all over when timeNow - startTime > time

    If the time is in millisecond or smaller you will need to convert it to seconds by dividing. Ie for milliseconds time = timeInMillisecond / 1000;