The class Object

I’m trying to write a simple 2D physics simulator. In it, I have 3 classes : One for box, one for circle, and one called manifold which serve for collision detection.
Long story short, the manifold class need to have 2 variable of the thing that colide inside that can either be a box or circle. I found out that instead of having 2 variable box and 2 variable circle and not declare some of them (which would be increadibly messy) I could have 2 variable of the type Object, that can be one or the other without any problem.

The thing is, when I try to access some value/field inside of those 2 Object, for exemple var1.pos, I get an error saying that ‘the global variable “pos” does not exist’.

How could I still access the values inside of those Object variable, and how should I use these to be as the class they actually are ?

1 Like

I suspect you are doing it right, but are having trouble because there’s already an “Object” class defined that’s hidden away via Processing’s magic which is having a name collision wth the Object class you are defining.

If you rename your “Object” class to “MyObject”, in all the places you use it, do you still have the same problem?

1 Like

I have created no “Object” class, i’m just using the native Java Object class wich is the root of all class / type of variable, so you can more or less put everything here no matter the type/class of the variable/Object. I guess that I’m doing this wrong or that I don’t understand how you’re supposed to use it.

Without seeing the (possible sample/example) code of your problem, it’s hard to say what is wrong.

The native Java Object class doesn’t have a “pos” attribute! How/why do you think one is added/defined?

Also consider:

class MyShape {
  boolean is_circle;
  float x, y;
  MyShape(){
    x = random(width);
    y = random(height);
    is_circle = random(1) < .5;
  }
  void render(){
    if( is_circle ){
      ellipse(x,y,20,20);
    } else {
      rect(x-10,y-10,20,20);
    }
  }
}
2 Likes
Object a = new manifold();
Object b = new box();
Object c = new circle();

Are all valid statements but what data type are they?

In fact they are all of type Object so when you do something like

a.checkCollision(b, c);

It won’t work because checkCollision is a method inside the mmanifold class and does not exist.

((manifold)a).checkCollision(b, c);

will work because you have cast the variable to the correct class.

The same applies to variable a and c they can only access the methods available in the java.lang.Object class.

3 Likes

The solution is to either use inheritance or interfaces so that objects of type circle and box look the same to the manifold class but have different implementations for common methods.

3 Likes

well because I did something like
Object a = new box()
and because box was defined as having a “pos” attribute. But I guess that don’t really make sens.
I’ll probably do the option of combining them under one class as you’ve shown, but i’ll avoid doing that if I can fine an other way around keeping them as separate classes.
(I don’t know how to indent the code in my post, sorry)

Oh yeah this totally work and is still pretty clean ! thanks a lot !

Not sure what you got to work but this minimal sketch shows how it can be done with inheritance. If you run the sketch you will see that the manifold method will accept 2 objects of type Shapes2D but inside the method we can call methods in the actual class.

public void setup() {
  Manifold m = new Manifold();
  Circle c = new Circle(60, 80, 25);
  Box b = new Box(20,40, 115, 55);
  m.haveCollided(b, c);
}

class Manifold {

  boolean haveCollided(Shape2D s0, Shape2D s1) {
    s0.desc();
    s1.desc();
    return false;
  }
}

abstract class Shape2D {
  float x;
  float y;

  Shape2D(float x, float y) {
    this.x = x;
    this.y = y;
  }

  abstract void desc();
}

class Box extends Shape2D {

  float w;
  float h;
  Box(float x, float y, float w, float h) {
    super(x, y);
    this.w = w;
    this.h = h;
  }

  void desc() {
    println("Box @", x, y, "size=", w, h);
  }
}

class Circle extends Shape2D {

  float r;

  Circle(float x, float y, float r) {
    super(x, y);
    this.r = r;
  }

  void desc() {
    println("Circle @ ", x, y, "radius = ", r);
  }
}
4 Likes

This could be a good method but for the same reason that I don’t want to fusion the two classes, i think i’ll stay with the casting on an Object variable plus the use of if( myShapeVar instanceof Box) to determine the actual class of the shape. I’m not really used to the how class work, particularly the inheritance, but i’ll think about it if I get stuck again over a problem like that, thanks !

That’s fine, do what you are comfortable with.

I do recommend that you learn the object orientated paradigm at some time it makes similar projects so much easier to create, edit and maintain.

3 Likes