We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I'm working on a Physics engine with POO (Python Object Oriented). I have an object problem with a certain "call" attribute.
I have a main file and a file called Physics.py. In Physics.py, I have 3 classes: Object, Vector and Rigidbody.
This is the essential properties of my Object class:
class Object:
def __init__(self, x, y, m):
self.rigidbody = Rigidbody(x, y, m)
self.s = 10
This is the essential properties of my Rigidbody class:
class Rigidbody:
def __init__(self, x, y, m):
self.x = x
self.y = y
self.m = m # m is unused yet...
self.velocity = Vector(0, 0)
self.force = Vector(0, 0)
self.acceleration = 1
def initVelocity(self, x, y):
self.velocity = Vector(x, y)
def force(self, x, y):
self.force = Vector(x, y)
This is the essential properties of my Vector class:
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
self.l = sqrt(x**2+y**2)
Behind, I have functions like self.display() that displays a vector or an object on the screen with basic drawing functions. Finally, here is my main file:
from Physics import *
obj = Object(640, 360, 1)
def setup():
size(1280, 720)
frameRate(60)
smooth()
global obj
obj.defSize(50)
obj.defColor([255])
def draw():
global obj
background(50)
obj.rigidbody.force(100, 0)
obj.display() # This is the display function that I told higher
The force object in the class Rigidbody has other things, but this line in the object is the source of this error:
AttributeError: Vector instance has no attribute '__call__'
When the error comes, Processing highlights this line of code:
obj.rigidbody.force(100, 0)
which is in the def draw(). But when I print callable(Vector) it writes True! I don't understand why I have to add an attribute call to my class, because in my def force(self, x, y), I define the self.force as a Vector object! Is there a problem? Am I calling the Vector instance as a function? Please, I need help! [-O<
Answers
Wait, it was just a name problem... I choose the same name for the function force and the self.force property... I renamed the function addforce and it works!
# https://forum.Processing.org/two/discussion/21766/ # i-have-a-problem-with-a-call-attribute#Item_2 # 2017-Apr-02 #add_library('Physics') def setup(): size(300, 300) fill(0xffFFFF00) stroke(0) strokeWeight(3) ellipseMode(CENTER) global obj obj = Object(width>>1, height>>1, height>>1) obj.rigidBody.initForce(100, 0) def draw(): background(050) obj.display() class Object: def __init__(self, x, y, m): self.rigidBody = RigidBody(x, y, m) self.s = 10 self.rigidBody.initForce(100, 0) def display(self): rb = self.rigidBody ellipse(rb.x, rb.y, rb.m, rb.m) class Vector: def __init__(self, x=0, y=0): self.setXY(x, y) self.l = sqrt(x*x + y*y) def setXY(self, x, y): self.x, self.y = x, y class RigidBody: def __init__(self, x=0, y=0, m=0): self.x, self.y, self.m = x, y, m self.velocity, self.force = Vector(), Vector() self.acceleration = 1 def initVelocity(self, x, y): self.velocity.setXY(x, y) def initForce(self, x, y): self.force.setXY(x, y)Oops! You've already solved it 20 min ago. I was too l8! :\">
xD