How to use 'keypressfcn' for making a diffrence?

Hi guys. I have a question.
this is an octave code, prior to start.
making a code which like 3-D simulation. even its scientific accuracy is worst
function orbit
clc; clear all; close all;
% Defining specific physical quntaties.
m1 = 1; m2 = 1E+11;
G = 6.67384*10^(-11); t=0; dt = 1;
p1 = [1E+2, 0, 0];
p2 = [0, 0, 0];
v1 = [0, sqrt((G*m2)/(p1(1)-p2(1))), 0.05] ;
v2 = [0, 0, 0];
% Generating graphic objects.
% o1 = simulating obj
% o2 = center massive bulk
o1p = line('xdata', p1(1), 'ydata', p1(2), 'zdata', p1(3), 'marker', 'o', 'color', 'r');
r1 = 10;
n1 = 10;
phi1 = (-n1:1:n1)'/n1*pi;
theta1 = (-n1:1:n1)/n1*2*pi;
u1 = r1*cos(phi1);
x1 = sqrt(r1^2-u1.^2)*cos(theta1);
y1 = sqrt(r1^2-u1.^2)*sin(theta1);
z1 = repmat(u1,1,length(phi1));
o1s = surf(x1+p1(1), y1+p1(2), z1+p1(3));
r2 = 30;
n2 = 10;
phi2 = (-n2:1:n2)'/n2*pi;
theta2 = (-n2:1:n2)/n2*2*pi;
u2 = r2*cos(phi2);
x2 = sqrt(r2^2-u2.^2)*cos(theta2);
y2 = sqrt(r2^2-u2.^2)*sin(theta2);
z2 = repmat(u2,1,length(phi2));
o2 = surf(x2, y2, z2);
% equation of motion.
while collision == 0
dist = sqrt((p2(1)-p1(1))^2 + (p2(2)-p1(2))^2 + (p2(3)-p1(3))^2);
uvec12 = [p2(1)-p1(1), p2(2)-p1(2), p2(3)-p1(3)]*(1/dist);
uvec21 = -uvec12;
absF = G*m1*m2/((dist)^2);
F1 = absF*uvec12;
F2 = absF*uvec21;
dv1 = (F1/m1)*dt;
p1 = p1 + v1*dt;
v1 = v1 + dv1*dt;
absv = sqrt(v1(1)^2 + v1(2)^2 + v1(3)^2);
uvec_v = v1/absv;
% chek the movement through o1, auto-scaled 3D view and xy-plane.
axes(axFPS)
set(o1s, 'xdata', x1+p1(1), 'ydata', y1+p1(2), 'zdata', z1+p1(3));
campos([p1(1)-uvec_v(1), p1(2)-uvec_v(2), p1(3)-uvec_v(3)]);
camup([0, 0, 1]);
view([p1(1), p1(2), p1(3)]);
axes(axTPS)
o1p = line('xdata', p1(1), 'ydata', p1(2), 'zdata', p1(3), 'marker', 'o');
xlim([-200,200]); ylim([-200,200]); zlim([-200, 200]);
view(3);
axes(axxy)
o1p = line('xdata', p1(1), 'ydata', p1(2), 'zdata', p1(3), 'marker', 'o');
xlim([-200,200]); ylim([-200,200]); zlim([-200, 200]);
view(0, 90);
drawnow;
end
accutally, i want to put 'some acceleration' .
if i press 'space bar', then the v1 = v1 + v1*(5E-2). % toward its direction
i press 'c', then the v1 = v1 - v1*(5E-2) % deceleration.
i press 'a' , then v1 = v1 + [-v1(2), v1(1)]*(5E-2) % left to the direction of tavel.
i press 'd', then v1 = v1 + [v1(2), -v1(1)]*(5E-2) % right to the direction of travel
i attempt to use 'keypressfcn', in the while loop
but it doesn't work well. what can i do?
hope you have a nice day. thank you for reading guys.
while colllision == 0;
...
set(fig ,'keypressfcn', @move_coltrol)
endwhile
% another script, named 'move_control.m'
function move_control(scr, eventdata)
k = eventdata.Key
switch k
case 'space'
t = t + dt;
v1 = v1 + v1*(5E-2);
p1 = p1 + v1*dt;
case 'c'
t = t + dt;
v1 = v1 - v1*(5E-2);
p1 = p1 + v1*dt;
% 'a' & 'd' accelerate only 'x, y velocity'
case 'a'
t = t + dt;
v1 = v1 + [-v1(2), v1(1), 0]*(5E-2);
p1 = p1 + v1*dt;
case 'd'
t = t + dt;
v1 = v1 + [v1(2), -v1(1), 0]*(5E-2);
endswitch
endfunction

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 9 Dec 2018

Edited:

on 9 Dec 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!