Ticks & Random Movement

Gah, sorry for all the nooby questions. One day I’ll give something back by helping some noobs when I’m more experienced x3

Hey, I was wondering how to use ‘ticks’ so that whenever my FPS goes down, the movement doesn’t slow down. I know it’s probably simple, but I don’t get it xP

I was also wondering if I could make something move in a random way for a random amount of time, like a small ‘AI’.

Thanks!

just define a tick rates, for example you want 100 logic steps(ticks) per second.
so just save the time you started your simulation(game) and everytime you come around check the time and see howmany ticks you should have done until know and how many you already did, the delta of both values is the amount of new ticks to calculate.

like:


getAmountOfNewTicks()
{
   long time = getTime();
   long elapsedTime = time - START_TIME;
   int totalTicks = elapsedTime / TIME_PER_TICK;
   int newTicks = totalTicks - elapsedTicks;
   elapsedTicks = totalTicks;
   return newTicks;
}
}

Thanks, Danny, but I honestly don’t understand a word of that xD
Is there anyone that can explain it to a noob?

@Danny02
I do believe the OP actually wants a variable game loop

@OP
Read this post to understand how variable game loops work.

The Random class is really great.
I just came up with this while trying to make random movement of a box:


	/**
	 * Used to create 2 random numbers and assign them to what you wish.
	 * @param randomMaximumX max number for the first integer x.
	 * @param randomMaximumY max number for the second integer y.
	 */
	public static void randomPlacement(int randomMaximumX, int randomMaximumY) {
		Random r = new Random();
		int x, y;
		x = r.nextInt(randomMaximumX);
		y = r.nextInt(randomMaximumY);
		System.out.println("2 Random x/y coordinates: X: ["+x+"], Y: ["+y+"].");
	}

Hope it helps mate.

Don’t create a new Random object each time, instead store it in a global variable or use Math.random().

Don’t use Math.random() it’s synzronized so it a bit slower.(about 2-3 times)

lmfao ra4king.
Everything i say you come along with a ‘Don’t do that…’.
I code how I code, it gets things done how i want it to be done, if I did notice something that ‘absolutely shouldn’t be done’ was done, I’d fix it.
8/10 T.T :o

@GabrielBailey74: If you “code how you code” then keep it for yourself and dont post bad code on the forums.
Otherwise stop whining if you get corrected.

Yes, the random object is how you would go about doing random things. Like the above poster said.

And ra4king is right in that you should not create a new object every time you want to do a randomized thing so I would have maybe a static random object in your class or a class that just has a random object in it that you can call for random numbers.

@ GabrielBailey74
I think ra4king just wants to make sure people don’t make mistakes so they won’t repeat those mistakes later on. Yes it is good to have something working before you make sure it works well but don’t be hard on him for helping.

@ Kerai
Calm down no need to go into attack mode.

Math.random() is not synchronized.

Creating a new Random object each time the method is called is not “bad practice”, it will lead to undesired side effects, such as the same number being produced more than once if that method is called multiple times in the same millisecond :wink:

Ehhhh welcome to JGO! ;D

@GabrielBailey73
I post useful and helpful advice, but you don’t have to listen to me. :wink:

“Don’t create a new Random object each time”.
If you look at the code it was for random x,y coordinates, obviously every time its ran through that method it’s going to need a new ‘Random()’ to assign RANDOM variables to it right?..

There is actually nothing random about the Random class. :slight_smile:
It works using a seed, which by default is System.currentTimeMillis(). The next() methods then do a bunch of complicated math and bit shifts, assign an updated seed, then returns a seemingly random number.

Using the same seed gets you the same sequence of “random” numbers :wink:

Try running this code:


Random r1 = new Random()
Random r2 = new Random(System.currentTimeMillis());

for(int a = 0; a < 10; a++)
    System.out.println(r1.nextInt(100) + " " + r2.nextInt(100));

99.9% of the time you will get the same value from both.
The other .1% is the rare chance that the first line is run in a different millisecond from the second line :slight_smile:

Note to self: stop writing code using cramped phone keyboard T__T

D:< Well i’ve just been educated :o.
I know I’d like to ‘attempt’ to make a 2D tower defense game, know I’m going to need some tips. (make a post)

Check out updated post :slight_smile:

I stand corrected. I checked the code and only initRng() is synchronized.

Official Javadoc and random internet comments made me think otherwise.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random()


This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator. 

But synchronization comes from staticness not from the keyword.
But it’s still slower. one null check and one method call more.

Yeah that’s about 1 nanosecond slower…compared to many hundreds of nanoseconds for creating a Random object ;D

EDIT: Oh and please use the Java 6 or 7 API, 1.4.2 needs to die already >.> Just replace “1.4.2” with “6” or “7” (without the quotes of course)

WHAT ARE YOU DOING!!?

Needs to be:


Oh and please use the Java 
(int) (6) or (int) (7) API, (double) (1.4.2) needs to die already >.> 
Just replace (String) ("1.4.2") with (String) ("6") or (String) ("7") 
(without the quotes of course)

lol ra4king… XD >:( >:(

O______O XD ;D

The java builtin random number generator is, frankly, stupid. The concurrent syncs don’t really serve any useful purpose. (EDIT: nuked semi-rant) (EDIT of EDIT: I can’t spell).