How can I create a visual simulation that is controlled by the keyboard and displayed on screen?

5 Ansichten (letzte 30 Tage)
I would like to create a simulation whose parameters can be controlled using a keyboard in near real-time. For example I would like to move a ball around the screen and control its velocity using the four arrow keys. Hitting the Up Arrow key would increase the velocity in the upward direction and so on. The ball would move on-screen and would respond to my key presses.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 10 Nov. 2025
Bearbeitet: MathWorks Support Team am 10 Nov. 2025
A combination of tools is needed to achieve this.
The first is to be able to register key presses. To do this, register the KeyPressFcn to the figure window which contains the drawing surface. This allows the figure to 'listen' to key presses and then perform a task when a key is pressed. A function referred to as the Event Callback is then called. This function would check which key was pressed and would update the ball’s velocity.
The different callbacks are described here:https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-apps-created-programmatically.html
The callbacks available for a figure are described here:https://www.mathworks.com/help/matlab/ref/matlab.ui.figure.html
The second requirement is to update the position of the ball on the screen. This has to be done periodically to allow smooth motion of the ball. This can be done by setting up a Timer object. This object would periodically call a function (referred to as the Timer Function) which would refresh the position of the ball and draw it to the screen.
Details on the Timer object can be found here:https://www.mathworks.com/help/matlab/ref/timer.html
Finally since event-based techniques are being used, a method to transfer data, such as current velocity and position, between the callbacks is needed. One method is to store the data in the UserData property of the figure. Then you can extract this information as needed or update the stored information within the callbacks. The GET and SET functions can be used for this purpose.
Details on sharing data between callback can be found here:https://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
The attached function called motion_example.m demonstrates the moving user-controlled ball. Download and place the file in a directory and call it as follows:
motion_example
from the MATLAB command prompt. Use the arrow keys on the keyboard to change the velocity. You must monitor the ball’s velocity at the top of the window. If the ball hits the edge of the figure it will ‘stick’ to the wall. If this happens you will need to change the velocity to the other direction to move it away.

Weitere Antworten (0)

Kategorien

Mehr zu Desktop finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by