class: how to use the class within itself
Show older comments
Hi there, I have used Processing (based on java) and recently turned to Matlab, and its syntax is quiet confusing..I am trying to make a simple spring simulation using class, and in the function 'reaction', I would like to use the object from this class as an input, and in the function 'move', I'd like to use the property 'position' initialised in the beginning and change it constantly.
But when I call this class, millions of errors occurs..can anybody help to tell me where I did wrong..I got something saying that the property 'position'must be accessed from a class instance because it is not a constant property..And I've never seen that..
classdef Particle
properties
rest = 100;
mass=1000;
damping=0.999;
position=[0 0];
velocity; %= [1.*rand(100,1) 1.*rand(100,1)];
end
methods (Static)
function this = Particle()
this.position=[0 0];
this.velocity=[1.*rand(100,1) 1.*rand(100,1)];
end
function velocity=react(Particle)
p1 = Particle.position;
p2 = Particle.position;
d = sqrt((p1(1)-p2(1))^2+(p1(2)-p2(2))^2);
displacement = d-Particle.rest;
a = normr(p1-p2);
a = a.*(displacement/Particle.mass);
velocity = Particle.velocity+a;
% Particle.bounce(position, velocity);
velocity = velocity.*Particle.damping;
end
function [position] = move (velocity)
position=Particle.this.position+velocity;
end
function draw (myposition)
viscircles(myposition, 0.1);
end
end
end
and this is my main sketch calling class
a = Particle;
b = Particle;
a.react(b);
b.react(a);
a.move();
b.move();
line([a.position(1) b.position(1)],[a.position(2) b.position(2)]);
a.draw();
b.draw();
Thanks so much! I think I'm still using the java syntax rather than matlab's....
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!