We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone !
I'm really excited to start using the toxiclibs library following this Daniel Shifmann tutorial but i'm having hard times implementing the following snippet in Python:
class Particle extends VerletParticle2D {
Particle(float x, float y) {
super(x, y);
}
void display() {
fill(255);
ellipse(x, y, 10, 10);
}
}
This is supposed to get the x and y coordinates from the main .pde file and make them "move"/'"change" according to the toxiclibs VerletPhysics engine.
I believe the corresponding Python code would be:
import toxi.physics2d.VerletParticle2D as VerletParticle2D
class Particle(VerletParticle2D):
def __init__(self, x, y):
super(Particle, self).__init__(x, y)
self.x = x
self.y = y
def display(self):
fill(255)
ellipse(self.x, self.y, 10, 10)
But I must have missed something because the coordinates don't change and all my points stay still on the screen. Do you have any idea what I'm doing wrong here ?
Answers
Thanks for your answer GoToLoop. I must admit I don't really understand how this code could help me. I need to get the coordinates from the main .pyde file so I think this part is mendatory (?) :
Now, following that logic I should then write:
def display(self):
#'self' is important right ?Below the main .pyde file:
AFAIK, the way I've refactored your Particle class should work alright. :ar!
The console output from:
print Particle(width>>1, height>>1).display()
as
{x:50.0, y:50.0}
proves that the class had been properly initialized. :-BWhy don't you test it the way it is now, then make your own changes to it later? :>
I did ! I'm still fiddling with your code right now, trying to figure this out.
Version 1.2 now:
x, y = p.x(), p.y(); print p, x, y
prints out:{x:50.0, y:50.0} 50.0 50.0
B-)Sorry still don't get it, I must be dumb. How can I specify x and y IN the inheritance part ?
ToxicVerletParticle2D.pyde:
Particle.py:
If I can get away w/ it, I very much prefer not to implement my own
__init__()
when extending a class. 8-|In particular, b/c parent class VerletParticle2D got both fields x & y, plus their corresponding getters x() & y(), that forces Jython to have to choose either the fields or the getter methods. :-SS
It is clear to me that Jython picked the getters x() & y() over the fields x & y. :-\"
Your code surely works but whenever I try to apply to my sketch it doesn't work at all. Thanks for your time anyway.
Well, could you at least post your attempt to use my own Particle class version? L-)
Also, I don't think the Toxic library can be imported via add_library()! :-&
The thing is my code initialize the class but nothing moves. At first I thought it had to do with my initialization but now I come to think it has something to do with the Python mode not fully working with the toxiclibs library
I'm still curious to know what would have been your version of the extension using
__init()__
Ru doing more than passing fields x & y inside your
__init__()
? :-/In case you're not, there's no advantage on implementing your own
__init__()
. :-@Like I've already asked, w/o seeing how you would attempt to use my own Particle implementation, it's hard to have an exact idea what's going wrong! #-o
Got it after an hour messing with your answer:
Just that simple ! I feel really dumb tbh.
Thank you GoToLoop !
I've merged the code you've provided w/ my own subclass Particle. :-bd
Also, I've added an
__init__()
constructor w/ super() for my subclass Particle too: :bzdef __init__(p, *args): super(Particle, p).__init__(*args)
It's been posted commented out b/c it's actually redundant & unnecessary. :-\"
But you can remove the
#
there if you wish to check that out for yourself. :>ToxicVerletParticle2D.pyde:
Particle.py:
Beautiful ! I'm going to test this right now. Many thanks !
Latest "Toxic VerletParticle2D" version 3.0; now based on the original Daniel Shifman's "Cloth2d" Java Mode sketch from Coding Train Challenge YouTube tutorial: \m/
https://GitHub.com/CodingTrain/website/tree/master/CodingChallenges/CC_020_Cloth2D
ToxicVerletParticle2D.pyde:
Particle.py:
Spring.py:
That's neat.
I'd like to add a mouseEvent (to drag the particles with the mouse and see the distortion) in the Particle class of this sketch (my version) but can't figure how to make it work. Should I post a new question or keep asking here ?
Yea, you may choose to start a new forum thread for it.
But given it's related to the stuff here, also post a link to this thread there.
Will do thanks.
Given Toxiblibs is also available under JavaScript as well: ~O)
http://Haptic-Data.com/toxiclibsjs
https://GitHub.com/hapticdata/toxiclibsjs
I've converted "Toxic VerletParticle2D" to p5.js too: https://p5js.org/ B-)
And you all can check it online here: :bz
https://Bl.ocks.org/GoSubRoutine/f4e383cfc4de8b063df3ee1c3fc66419
index.html:
Particle.js:
Spring.js:
sketch.js: