Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ticks & Random Movement  (Read 4612 times)
0 Members and 1 Guest are viewing this topic.
Offline Quentin

Senior Newbie




Big newb


« Posted 2012-02-24 06:43:56 »

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!
Online Danny02
« Reply #1 - Posted 2012-02-24 10:37:37 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
getAmountOfNewTicks()
{
   long time = getTime();
   long elapsedTime = time - START_TIME;
   int totalTicks = elapsedTime / TIME_PER_TICK;
   int newTicks = totalTicks - elapsedTicks;
   elapsedTicks = totalTicks;
   return newTicks;
}
}
Offline Quentin

Senior Newbie




Big newb


« Reply #2 - Posted 2012-02-25 03:34:21 »

Thanks, Danny, but I honestly don't understand a word of that xD
Is there anyone that can explain it to a noob?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-02-25 04:17:58 »

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

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

Offline GabrielBailey74
« Reply #4 - Posted 2012-02-25 06:08:21 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   /**
    * 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.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-02-25 08:34:49 »

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

Offline pitbuller
« Reply #6 - Posted 2012-02-25 09:46:04 »

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)
Offline GabrielBailey74
« Reply #7 - Posted 2012-03-12 03:03:10 »

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  Shocked

Offline Kerai

Junior Member


Medals: 4



« Reply #8 - Posted 2012-03-12 03:17:06 »

@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.
Offline StumpyStrust
« Reply #9 - Posted 2012-03-12 03:32:17 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-03-12 03:35:12 »

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

@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.
Ehhhh welcome to JGO! Grin

@GabrielBailey73
I post useful and helpful advice, but you don't have to listen to me. Wink

Offline GabrielBailey74
« Reply #11 - Posted 2012-03-12 05:14:49 »

"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?..


Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2012-03-12 05:20:20 »

There is actually nothing random about the Random class. Smiley
It works using a seed, which by default is System.currentTimeMillis(). The next<xxx>() 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:
1  
2  
3  
4  
5  
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 Smiley

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

Offline GabrielBailey74
« Reply #13 - Posted 2012-03-12 05:25:51 »

D:< Well i've just been educated  Shocked.
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)

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-03-12 05:27:44 »

Check out updated post Smiley

Offline pitbuller
« Reply #15 - Posted 2012-03-12 06:42:46 »

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)
Math.random() is not synchronized.

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%28%29
1  
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.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-03-12 06:44:44 »

Yeah that's about 1 nanosecond slower......compared to many hundreds of nanoseconds for creating a Random object Grin

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)

Offline GabrielBailey74
« Reply #17 - Posted 2012-03-12 07:11:37 »

WHAT ARE YOU DOING!!?

Needs to be:
1  
2  
3  
4  
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 Angry Angry

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-03-12 07:14:37 »

O______O XD Grin

Offline Roquen
« Reply #19 - Posted 2012-03-12 10:50:40 »

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).
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #20 - Posted 2012-03-12 13:01:07 »

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)

<OT>
Funny enough, Googling for different Java classes by name gives different Java releases' Javadocs for the #1 slot.  For example:

"java Math" => Java 6
"java Math class" => Java 1.4.2 (!)
"java Proxy" => 1.4.2
"java ArrayList" => 5
"java XmlReader" => 1.4.2
"java Cipher" => 1.4.2
"java ArrayList" => 5

It's surprising how often 1.4.2's documentation is the #1 result, with Java 6, or even 5, being the #2 suggestion.  Especially considering Java 5's been out for over 7 years now.
</OT>
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2012-03-12 18:09:53 »

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)

<OT>
Funny enough, Googling for different Java classes by name gives different Java releases' Javadocs for the #1 slot.  For example:

"java Math" => Java 6
"java Math class" => Java 1.4.2 (!)
"java Proxy" => 1.4.2
"java ArrayList" => 5
"java XmlReader" => 1.4.2
"java Cipher" => 1.4.2
"java ArrayList" => 5

It's surprising how often 1.4.2's documentation is the #1 result, with Java 6, or even 5, being the #2 suggestion.  Especially considering Java 5's been out for over 7 years now.
</OT>

Yeh and this wont end any time soon since most people don't care and just click on the first link.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #22 - Posted 2012-03-12 21:01:57 »

Don't know if you are looking for random numbers inside of a range or not, but this is a great bit on that.

-Nathan

Check out my website!
Offline pitbuller
« Reply #23 - Posted 2012-03-12 21:56:18 »

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)

<OT>
Funny enough, Googling for different Java classes by name gives different Java releases' Javadocs for the #1 slot.  For example:

"java Math" => Java 6
"java Math class" => Java 1.4.2 (!)
"java Proxy" => 1.4.2
"java ArrayList" => 5
"java XmlReader" => 1.4.2
"java Cipher" => 1.4.2
"java ArrayList" => 5

It's surprising how often 1.4.2's documentation is the #1 result, with Java 6, or even 5, being the #2 suggestion.  Especially considering Java 5's been out for over 7 years now.
</OT>

Yeh and this wont end any time soon since most people don't care and just click on the first link.

For math.random() javadoc is indentic with 1.4.2 and 7. I checked this but copied wrong link.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!