Quentin
Senior Newbie 
Big newb
|
 |
«
Posted
2012-02-24 07: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!
|
|
|
|
|
Danny02
|
 |
«
Reply #1 - Posted
2012-02-24 11: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; } } |
|
|
|
|
|
Quentin
Senior Newbie 
Big newb
|
 |
«
Reply #2 - Posted
2012-02-25 04: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!
|
|
|
|
GabrielBailey74
|
 |
«
Reply #4 - Posted
2012-02-25 07: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
| 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.
|
|
|
|
ra4king
|
 |
«
Reply #5 - Posted
2012-02-25 09:34:49 » |
|
Don't create a new Random object each time, instead store it in a global variable or use Math.random().
|
|
|
|
pitbuller
|
 |
«
Reply #6 - Posted
2012-02-25 10: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)
|
|
|
|
|
GabrielBailey74
|
 |
«
Reply #7 - Posted
2012-03-12 04: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 
|
|
|
|
Kerai
Senior Newbie  Medals: 2
|
 |
«
Reply #8 - Posted
2012-03-12 04: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.
|
|
|
|
|
StumpyStrust
|
 |
«
Reply #9 - Posted
2012-03-12 04: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!
|
|
ra4king
|
 |
«
Reply #10 - Posted
2012-03-12 04: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  @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!  @GabrielBailey73 I post useful and helpful advice, but you don't have to listen to me. 
|
|
|
|
GabrielBailey74
|
 |
«
Reply #11 - Posted
2012-03-12 06: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?..
|
|
|
|
ra4king
|
 |
«
Reply #12 - Posted
2012-03-12 06:20:20 » |
|
There is actually nothing random about the Random class.  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  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  Note to self: stop writing code using cramped phone keyboard T__T
|
|
|
|
GabrielBailey74
|
 |
«
Reply #13 - Posted
2012-03-12 06:25:51 » |
|
D:< Well i've just been educated  . 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)
|
|
|
|
ra4king
|
 |
«
Reply #14 - Posted
2012-03-12 06:27:44 » |
|
Check out updated post 
|
|
|
|
pitbuller
|
 |
«
Reply #15 - Posted
2012-03-12 07: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%291
| 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.
|
|
|
|
|
ra4king
|
 |
«
Reply #16 - Posted
2012-03-12 07:44:44 » |
|
Yeah that's about 1 nanosecond slower......compared to many hundreds of nanoseconds for creating a Random object  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)
|
|
|
|
GabrielBailey74
|
 |
«
Reply #17 - Posted
2012-03-12 08: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 
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2012-03-12 08:14:37 » |
|
O______O XD 
|
|
|
|
Roquen
|
 |
«
Reply #19 - Posted
2012-03-12 11: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).
|
|
|
|
|
BoBear2681
|
 |
«
Reply #20 - Posted
2012-03-12 14: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>
|
|
|
|
|
ra4king
|
 |
«
Reply #21 - Posted
2012-03-12 19: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.
|
|
|
|
StonePickaxes
|
 |
«
Reply #22 - Posted
2012-03-12 22: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
|
|
|
|
pitbuller
|
 |
«
Reply #23 - Posted
2012-03-12 22: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.
|
|
|
|
|
|