Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
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  
  Geometry classes  (Read 635 times)
0 Members and 1 Guest are viewing this topic.
Offline divxdede

Junior Member





« Posted 2013-01-26 16:47:04 »

I would discuss about a small geometry implementation:

Consider having all basic classes like Point, Dimension, Rectangle, Circle.

By example, you will find on a Circle a method like :
1  
public Point getCenter();


What would be your options ?:
  - Having "Point" immutable that let you the liberty to return the Point from the Circle object without any risk that somebody change it.
  - Having "Point" mutable that you must enforce in a some way any modifications (like with listeners). But even if you are able to intercept changes, theses changes may corrupt some objects states. (exemple changing corners of a rectangle that do not give a rectangle anymore).
  

At this time, my own geometry approche it to use immutable object for all objects. But it's not the best approche for reduce objects allocations. Each time i want to translate, rotate, ... a geometry objects, i must instanciate a new one...

The mutable approches is fine when you perform an animation and translate a series of geometry objects each update() or render() depending of what you do.

S├ębastien.
Offline Best Username Ever

Junior Member





« Reply #1 - Posted 2013-01-26 17:31:55 »

All of those things are simple enough to work with using primitives. Don't use extra objects for them if you don't have to.
Offline sproingie
« Reply #2 - Posted 2013-01-26 17:34:06 »

Immutable objects absolutely easier to reason about, no question.  And on the average desktop app, you're not likely to notice the difference if you create them by the millions.  But if you're creating them every frame on an Android machine, then you end up having to compromise and use mutable objects.

Seems to me modern platforms are pushing us backward Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-01-26 17:35:32 »

I think I came up with a good solution for this mutable immutable problem.

just create an interface for all the immutable methods of a primitive and only have mutable stuff in your implementation.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
interface ImmutableVector2{
  float getX();
  float getY();
  float dot(ImmutableVector2
}
class Vector2 implements ImmutableVector2
{
  public float x,y;
  void mul(float a)
  {
     x *= a; y*=a;
  }
}


so now when you want to have a method which returns a vector, but you don'T want that others can modify it just return a immutable one.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
class Circle
{
  private final Vector2 center;

  public Circle(Vector2 center)
  {
    this.center = center.clone();
  }

  public ImmutableVector2 getCenter(){return center;}
}
Offline Best Username Ever

Junior Member





« Reply #4 - Posted 2013-01-26 17:53:12 »

Immutable means "never changes". It does not mean read only. An type cannot mutable and immutable. A mutable class therefore cannot implement an interface that marks a class as being immutable.
Offline Danny02
« Reply #5 - Posted 2013-01-26 19:18:59 »

its a practical solution to enforce some constraints, so name it whatever you like  Cranky
Offline Best Username Ever

Junior Member





« Reply #6 - Posted 2013-01-26 19:35:49 »

No its not. An real immutable class would be practical. This class is broken. Immutability is a different (more practical) restraint than read only access. Name it ReadableVector if you use it, but it's still true that mutability and immutability or mutually exclusive.
Offline divxdede

Junior Member





« Reply #7 - Posted 2013-01-26 21:12:36 »

Immutable objects absolutely easier to reason about, no question.  And on the average desktop app, you're not likely to notice the difference if you create them by the millions.  But if you're creating them every frame on an Android machine, then you end up having to compromise and use mutable objects.

Seems to me modern platforms are pushing us backward Sad


I agree with you, the immutable approches is the more straightforward but not the most efficient in mobile platform.
I don't like primitives for storing theses kind of coordinates. when i saw a method like xxx(int x1,int y1, int x2 , int y2 , int x3 , int y3, int x4 , int y4 , ...) i have a headhache...
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.

xsi3rr4x (16 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (180 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!