Java Motion

0 views
Skip to first unread message

Madelyn Westfall

unread,
Aug 4, 2024, 11:37:05 PM8/4/24
to esadinet
InJava is it possible to register for a mouse motion event outside of a Java app? So if the mouse pointer moves anywhere on the screen I get a call back. An approximation is possible with polling MouseInfo.getPointerInfo but there must be a better way.

To explain the use case:It's just for a pet project but basically firing events when the mouse hits the edge of the screen. I was also thinking that different events could be fired if you try to push past the edge of the screen. And for this I thought a mouse motion listener might be more appropriate.


java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, there is no way around MouseInfo.getPointerInfo. However, you could write a (potentially singleton) class that polls the pointer info in regular intervals and allows MouseMotionListeners to be added:


Mouse-motion events notify when the user uses the mouse (or a similar input device) to move the onscreen cursor. For information on listening for other kinds of mouse events, such as clicks, see How to Write a Mouse Listener. For information on listening for mouse-wheel events, see How to Write a Mouse Wheel Listener.


If an application requires the detection of both mouse events and mouse-motion events, use the MouseInputAdapter class, which implements the MouseInputListener a convenient interface that implements both the MouseListener and MouseMotionListener interfaces.


The following demo code contains a mouse-motion listener. This demo is exactly the same as the demo described in the How to Write a Mouse Listener section, except for substituting the MouseMotionListener interface for the MouseListener interface. Additionally, MouseMotionEventDemo implements the mouseDragged and mouseMoved methods instead of the mouse listener methods, and displays coordinates instead of numbers of clicks.


The SelectionDemo example, draws a rectangle illustrating the user's current dragging. To draw the rectangle, the application must implement an event handler for three kinds of mouse events: mouse presses, mouse drags, and mouse releases. To be informed of all these events, the handler must implement both the MouseListener and MouseMotionListener interfaces, and be registered as both a mouse listener and a mouse-motion listener. To avoid having to define empty methods, the handler doesn't implement either listener interface directly. Instead, it extends MouseInputAdapter, as the following code snippet shows.


Im trying to get my movement smoother. At the moment when I press the up key it will go up and then when I press the left key it stops and then turns left. I want to make it so when i press right it will do it straight away and have no delay. How could I with my code


This architecture is roughly how most game engines work - they will handle user input using an event-driven architecture, but just maintain a giant map of which keys are currently down or not - that way it's easy and fast to query if a key is pressed deep within the render loop.


The game loop can be driven by a java.util.Timer operating at 60 fps. I use this rate as it is pretty common for monitors to refresh at 60 fps, so there are diminishing returns for going at a faster rate.


Some prefer to use a javax.swing.Timer but IDK if that is such a good idea. The EDT (event dispatch thread) can become something of a bottle neck. (The classic Java game programming book: Killer Game Programming in Java compares the two timers and the util Timer comes out the winner.) Arguably, a ScheduledThreadPoolExecutor is more robust than either, though it requires a bit of a learning curve if you haven't used Executors for multi-threading yet. (I prefer using JavaFX, so I use its AnimationTimer.)


The class holding the game loop should have public methods where the keyboard or mouse events that are triggered can simply set a variable or toggle a boolean (or add a variable to the top of a queue). The public input method for the keyboard can have a bit of extra processing to compare the incoming key event with current key state (e.g., a maintained map of key states as described elsewhere on this thread) and set a field for the game-loop thread to read and act upon.


This sort of architecture for a game program adds a great deal of predictability and control, as well as clarifying the logic of the various layers. It also takes good advantage of the multi-threading capabilities of today's processors.


What does the 4 minute delay relate to? Is the idea the lights that you've chosen to turn on are mean to turn off in that time? Or is the idea you are meant to have chosen lights in that "Options for Additional ...." section?


I have the similar errors but I have been told it's connected to hub connect possible due to earlier beta versions. I have to go through and delete and recreate them, but looks like that's not @djashones issue.


I had to do that as I tried per mode settings and it screwed it up totally. So now even more basic i.e. turn the lights when motion detected and turn them off after 4 mins and still the error messages.


Currently I have a class called Hero that has a CircleShape and when the user presses left/right/up/down it gets an linear impulse in the appropiate direction. All this works fine. The problem happens when the player does not produce any input, the shape is going further in the last direction for a long time, but I would like it if the shape stops faster.The players sees the world top down, so the worlds gravity is set to (0;0).


If you want to change the motion of an object, you need to add an additional force. In this case, a stopping force. This force is in the opposite direction of your velocity, with the sole purpose of arresting that velocity.


See the Box2D manual for more information (unfortunately, their manual is not really linkable to specific sections, so just search for "Friction is used to make objects slide along each other realistically").


To further help teams integrate motion profiling into their command-based robot projects, WPILib includes two convenience wrappers for the TrapezoidProfile class: TrapezoidProfileSubsystem, which automatically generates and executes motion profiles in its periodic() method, and the TrapezoidProfileCommand, which executes a single user-provided TrapezoidProfile.


In C++, the TrapezoidProfileSubsystem class is templated on the unit type used for distance measurements, which may be angular or linear. The passed-in values must have units consistent with the distance units, or a compile-time error will be thrown. For more information on C++ units, see The C++ Units Library.


The TrapezoidProfileSubsystem class (Java, C++) will automatically create and execute trapezoidal motion profiles to reach the user-provided goal state. To use the TrapezoidProfileSubsystem class, users must create a subclass of it.


Users must pass in a set of TrapezoidProfile.Constraints to the TrapezoidProfileSubsystem base class through the superclass constructor call of their subclass. This serves to constrain the automatically-generated profiles to a given maximum velocity and acceleration.


The setGoal() method can be used to set the goal state of the TrapezoidProfileSubsystem. The subsystem will automatically execute a profile to the goal, passing the current state at each iteration to the provided useState() method.


The enable() and disable() methods enable and disable the motion profiling control of the TrapezoidProfileSubsystem. When the subsystem is enabled, it will automatically run the control loop and call useState() periodically. When it is disabled, no control is performed.


In C++, the TrapezoidProfileCommand class is templated on the unit type used for distance measurements, which may be angular or linear. The passed-in values must have units consistent with the distance units, or a compile-time error will be thrown. For more information on C++ units, see The C++ Units Library.


A TrapezoidProfileCommand can be created two ways - by subclassing the TrapezoidProfileCommand class, or by defining the command inline. Both methods are ultimately extremely similar, and ultimately the choice of which to use comes down to where the user desires that the relevant code be located.


The output parameter is a function (usually passed as a lambda) that consumes the output and setpoint of the control loop. Passing in the useOutput function in PIDCommand is functionally analogous to overriding the useState() function in PIDSubsystem.


I have an another application which keep on capturing images from a fixed source in half second and stores it on file system. So I am passing two images to the opencv application and it will process them to detect motion between two frames.


I'm beginning to look at motion detection in java as well. There are a lot of powerful tools in OpenCV that can get you there rather quickly, as described at -lightenment.blogspot.com/2012/05/implementing-motion-detection-using.html


We have developed empirical ground motion relations for peak ground acceleration (PGA), peak ground velocity (PGV), and 5% damped Pseudospectral acceleration (PSA) at periods of 0.3, 1, and 3 seconds for earthquakes in both crustal and subduction zones in West Java, Indonesia. Our dataset comprises 1985 strong-motion recordings from 188 earthquakes including 60 crustal events and 128 subduction-zone events, with magnitudes ranging from 2.4 to 7.0 at regional distances of 9 to 300 km recorded since 2010. These predictive relations are crucial because the West Java region, characterized by a highly active seismic zone with the highest population density, lacks region-specific ground motion relations. Notably, existing relations such as Zhao et al. (2006), Campbell and Bozorgnia (2014), Bindi et al. (2017), and Ashadi and Kaka (2018) were found to be unreliable in predicting ground motion data in this region. Therefore, our research aimed to develop new empirical ground motion relations specifically for West Java. We assessed the validity of our relations by comparing observed ground-motions recordings for all crustal and subduction earthquakes reported in the region. In comparison to other existing relations, the relations developed in this study demonstrated significantly better predictive accuracy for both crustal and subduction earthquakes. Consequently, we recommend using these predictive relations as inputs for seismic hazard analysis and ShakeMap applications in the West Java region.

3a8082e126
Reply all
Reply to author
Forward
0 new messages