Creating a fish simulation in MATLAB

11 views (last 30 days)
I want to create a catch fish simulation in MATLAB for my research studies and so far I have been able to create a fish and add it to a figure handler. However, I am only limited only by 2 fishes. I am intending to put in around 30 of them. How can I put more of them? Here are my current code
classdef Fish < handle
%Constant properties
properties(Constant)
c_bounce=1;
radius = 1;
mass = 1;
gamma = 10;
loc = 5;
c = 5
d = 1;
end
%Private properties
properties(SetAccess = private)
fig;
axs;
pos;
vel;
accel;
item;
dt;
end
%Class method
methods
%Default constuctor
function this = Fish(fig,axs,dt)
this.fig = fig;
this.axs = axs;
this.pos = [randi([-50,50]), randi([0,50])];
this.vel = [0,10];
this.accel = [0,-10];
this.dt = dt;
this.item = rectangle('Position', [this.pos(1,1)-this.radius, this.pos(1,2)- this.radius, this.radius, this.radius],...
'Curvature',[1,1],...
'FaceColor', 'y',...
'Parent', this.axs);
end
function move(this)
this.pos = this.pos + this.vel* this.dt;
this.vel = this.vel + this.accel* this.dt;
end
function draw(this)
set(this.item,'Position',[this.pos(1,1)-this.radius,this.pos(1,2)-this.radius,...
this.radius,this.radius]);
axis(this.axs,[-50,...
50,...
0,...
50]);
end
function animate(this,dt)
while true
t_loopstart=tic();
move(this);
draw(this);
el_time=toc(t_loopstart);
pause(dt-el_time);
end
end
function collision(this)
% Set boundries
bottom = 0;
top = 50;
left = -25;
right = 25;
%--------------------------------------------------------------
if this.pos(1,2)-this.radius < bottom
this.vel(1,2)=-this.vel(1,2)*this.c_bounce;
end
if this.pos(1,2)+this.radius > top
this.vel(1,2)=-this.vel(1,2)*this.c_bounce;
end
if this.pos(1,1)-this.radius < left
this.vel(1,1)=-this.vel(1,1)*this.c_bounce;
end
if this.pos(1,1) + this.radius > right
this.vel(1,1)= -this.vel(1,1)*this.c_bounce;
end
end
end
end
%%END OF FISH CLASS
function main()
dt = 0.005;
[mainfig, mainaxs] = initField();
max = 5;
for i = 1:max;
school(i)=Fish(mainfig, mainaxs, dt);
end
%fish1 = Fish(mainfig, mainaxs, dt);
tb=uicontrol('style','togglebutton','string','Start','callback',@go,'units','normalized','position',[.025 .05 .1 .05],...
'backgroundcolor','w');
function go(varargin)
set(tb,'value',0)
if strcmp(get(tb,'string'),'Start')
set(tb,'string','Stop')
else
set(tb,'string','Start')
return
end
%axis equal;
while strcmp(get(tb,'string'),'Stop')
t_loopstart=tic();
for i = 1:max;
fish = school(i);
fish.move();
fish.collision();
end
redraw(school);
% fish1.move();
% fish1.collision();
% redraw(fish1);
el_time=toc(t_loopstart);
pause(dt-el_time);
end
end
function redraw(obj)
n = numel(obj);
for i = 1:n
fish = obj(i)
set(fish.item,'Position',[fish.pos(1,1)-fish.radius,...
fish.pos(1,2)-fish.radius,...
fish.radius,fish.radius]);
axis(fish.axs,[-50,...
50,...
0,...
50]);
end
end
end
function [fig, axs] = initField()
WinSize=[1,1,800,800];
ScrSize=get(0,'ScreenSize');
fig=figure('Name','Fish field simulation',...
'NumberTitle','off',...
'Menubar','none',...
'Units','pixels',...
'Position',[(ScrSize(3)-WinSize(3))/2,...
(ScrSize(4)-WinSize(4))/2,...
WinSize(3),WinSize(4)]);
axs=axes('Parent',fig,...
'Units','normalized',...
'Position',[0.1,0.1,0.8,0.8]);
axis(axs, [-50,...
50,...
0,...
50]);
end
I split it into two classes one is the fish class and one is the main. I realise that I can't pass in the figure to the fish class and let it run the animation by itself since I am going to add a second class ('fish hunter') into the mix. So I have to do the other way round adding the fish class to the figure and make the figure as the container for all the objects.Is there any efficient way of doing this?
TL;DR I am trying to add more fish to my animation but my computer just stop redrawing the object.

Answers (0)

Categories

Find more on Animation 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!