Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  Did you know? Hints and tips  (Read 2076 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2014-07-14 21:15:55 »

Thread created to share small, library independent (except OpenGL bindings) hints and tips. Please do not post obvious ones, let this topic become a valuable source of informations. Smiley

I will start with this one:

Did you know that you can use WeakReference objects to allow garbage collection of objects which are no longer accessible via "classic" references? In case of games, this can be useful for example in case of listeners.
More info/explanation: http://java.dzone.com/articles/reference-types-java-part-1
Offline GNecro1
« Reply #1 - Posted 2014-07-14 21:43:27 »

Some tips and tricks
http://www.alethis.net/reference/java/java.html

Java freak! Cheesy
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2014-07-14 21:48:13 »

I've posted these on the forum before, but I may as well do so again.
They're quite simple, but judging from the amount of people who don't know about them, maybe they're not so obvious.

1. You can cut AABB collision checks almost in half by changing the internals.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public class AABB
{
   public float x, y; // The centre of the AABB.
   public float w, h; // The extent (distance from centre to edge) of the AABB. Half of the actual width/height

   public boolean collides(AABB aabb)
   {
      if(Math.abs(x - aabb.x) < w + aabb.w)
      {
         if(Math.abs(y - aabb.y) < h + aabb.h)
         {
            return true;
         }
      }
      return false;
   }

   public boolean inside(float ox, float oy)
   {
      if(Math.abs(x - ox) < w)
      {
         if(Math.abs(y - oy) < h)
         {
            return true;
         }
      }
      return false;
   }
}

It's not actually as well known as it should be.

2. A reliable and cross-platform way to get the location of the executing jar file.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public static String jarDir()
   {
      try
      {
         return new File(SomeClass.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
      }
      catch(URISyntaxException e)
      {
         e.printStackTrace();
         return null;
      }
   }

And obviously the file of the jar itself can be obtained by removing the getParent() call.

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

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #3 - Posted 2014-07-14 22:21:39 »

I believe these versions are more succinct:   Roll Eyes  
P.S.: rx is w/2 & ry is h/2!

1  
2  
3  
public boolean isColliding(Actor a) { // CENTER coords.
  return x >= a.x-rx & x < a.x+rx & y >= a.y-ry & y < a.ry;
}


1  
2  
3  
public boolean isColliding(Actor a) { // CORNER coords.
  return x >= a.x & x < a.x+w & y >= a.y & y < a.y+h
}


Offline GoToLoop

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #4 - Posted 2014-07-14 22:23:50 »

Another good 1... Test for even or odd: (value & 1) == 0.
It's faster than value % 2 == 0!   Wink
Offline The Lion King
« Reply #5 - Posted 2014-07-14 22:29:41 »

Just off the top of my head these are ones I see all the time.
1  
2  
3  
4  
5  
if(randomBoolean == true)

//can be condensed to

if(randomBoolean)


also System.arraycopy  exists and is much faster than copying an array using a loop.

Also if you

import static java.lang.System;

you can print by simply doing

out.println

instead of

System.out.println

"You have to want it more than you want to breath, then you will be successful"
Offline GoToLoop

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #6 - Posted 2014-07-14 22:31:24 »

For circle shaped objects:

1  
2  
3  
public boolean isColliding(Actor a) { // CIRCLE shapes.
  return sq(a.x - x) + sq(a.y - y) < sq(a.rad + rad);
}


1  
2  
3  
public static final double sq(double val) {
  return val*val;
}
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #7 - Posted 2014-07-14 22:37:34 »

Another good 1... Test for even or odd: (value & 1) == 0.
It's faster than value % 2 == 0!   Wink

It's quite likely it's not faster.
At least not noticeably faster.
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2014-07-14 22:40:58 »

I believe these versions are more succinct:   Roll Eyes  
P.S.: rx is w/2 & ry is h/2!

1  
2  
3  
public boolean isColliding(Actor a) { // CENTER coords.
  return x >= a.x-rx & x < a.x+rx & y >= a.y-ry & y < a.ry;
}


1  
2  
3  
public boolean isColliding(Actor a) { // CORNER coords.
  return x >= a.x & x < a.x+w & y >= a.y & y < a.y+h
}



Those methods you posted are exactly the methods that are beaten ~2x by my code. Short code does not equal fast performance.

If you want the short versions:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public boolean collides(AABB aabb)
   {
      return Math.abs(x - aabb.x) < w + aabb.w && Math.abs(y - aabb.y) < h + aabb.h);
   }

   public boolean inside(float ox, float oy)
   {
      return Math.abs(x - ox) < w && Math.abs(y - oy) < h;
   }

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #9 - Posted 2014-07-14 22:49:20 »

If you're going for absolute performance, many of the Math arithmetic methods (min, max, abs, etc.) are usually intrinsified, and thus likely unbeatable in terms of speed. (disregarding high level optimizations)
Here's a list of known intrinsics: http://www.java-gaming.org/topics/hotspot-intrinsics/27010/view.html

However make sure you read the Math source, some methods (sin, cos, etc.) delegate to StrictMath and may not be intrinsified, which you could probably beat if you don't mind approximation.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Slyth2727
« Reply #10 - Posted 2014-07-14 23:25:25 »

You can initialize a List/Collection of any sort in one statement using double bracket initializers, like you can with arrays:

1  
2  
3  
4  
5  
6  
 Set<String> testSet = new HashSet<String>() {{
   add("valueA");
   add("valueB");
   add("valueC");
   add("valueD");
 }};


Useful!

Also, there's this really neat app that you can monitor your Java apps with called Java Visual VM or something like that. It comes with the JDK, look in jdkdir/bin/(something like jvisualvm).exe.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #11 - Posted 2014-07-14 23:31:07 »

You can initialize a List/Collection of any sort in one statement using double bracket initializers, like you can with arrays:
<snip>

Reasons this might not be a great idea: http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization

Some alleviation using Arrays:
List<String> s = Arrays.asList("1", "2");


Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (579 views)
2018-06-10 19:43:48

EgonOlsen (682 views)
2018-06-10 19:43:44

EgonOlsen (471 views)
2018-06-10 19:43:20

DesertCoockie (822 views)
2018-05-13 18:23:11

nelsongames (1025 views)
2018-04-24 18:15:36

nelsongames (1058 views)
2018-04-24 18:14:32

ivj94 (1638 views)
2018-03-24 14:47:39

ivj94 (572 views)
2018-03-24 14:46:31

ivj94 (1467 views)
2018-03-24 14:43:53

Solater (567 views)
2018-03-17 05:04:08
Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10
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!