Continuous MousePRessedEvent

Hi,
I need to move the camera on MousePressedEvent in such a manner that the camera keeps on moving while the Mouse Button is pressed,
The problem is that the event is fired only once i.e the camera moves only once although the button is pressed for a long period, how can I make it to be fired conituously.
I have seen this thing in J3D FlyThrough Demo, but I am failed to figure out the way it is implemented

Take Care

You don’t want to fire the mouse button event continuously. You want to move the camera on Mouse Dragged events.

You want a MouseMotionListener.

Hi,
Sir I am very much sure about my application’s requirment and I need to implement the camera movement on MousePressedEvent and not on the MouseMotion Event , you can try this thing in FlyThrough demo, the user dont have to drag the mouse to fly or Hoover, It is done on a single moue click.

Take good care of yourself.

Does not make too much sense to me. Flythrough use the common MouseBehaviours AFAIK.

For sure no ‘continuous MousePressedEvent’ - whatever that should be.

MouseDragged or MouseDown+MouseMotion are the ways to go.

You could always listen for MousePressed and MouseReleased and set a flag. Then in your behaviour that handles the tracking just check the flag.

Of course as stated above, if you do this when you press the mouse button and move the mouse you won’t have access to the new mouse pointer location but just the original press location.

Kev

If I was looking for the continuous mousepressed event I would follow kev’s suggestion, but be aware that sometimes Java3D can be busy and not always pick up an event, especially mouse or key release events for some reason, so you probably want to do some kind of poll to make sure it is still checked periodically.

[quote]If I was looking for the continuous mousepressed event I would follow kev’s suggestion,
[/quote]
Continuous MousePressed events make no sense. A MousePress event represents a change in the state of a mouse button… if you are holding it down the button is not changing state and therefore there should be no events. That is what a “Drag” is… use the interface for getting drag events.
You can monitor the state of the button with a flag as kev suggests, but you will still need a MouseMotionListener to get updates on the mouse cursor’s position… effectively you will re-write MouseDrag detection using slightly lower level events.

[quote]you can try this thing in FlyThrough demo, the user dont have to drag the mouse to fly or Hoover, It is done on a single moue click.
[/quote]
I find this statement confusing. Above you mentioned

[quote]the button is pressed for a long period
[/quote]
What is it a “click” - press and release, then move), or a “drag” - press and hold while moving?

[quote]Continuous MousePressed events make no sense.
[/quote]
You are quite right of course. I meant a continuous mousepressed effect based on mouse pressed and mouse released events. I maintain that there can be problems with release events being lost in J3D.

I think the guy in the first post wants something like a FPS game, when you have for instance the WASD keys for direction and while the player presses the key the avatar is moving. Except that he wants this with the mouse. Some game require the player to move the mouse to one of the borders of the screen and its there the mouse pointer changes to a left, right, direction arrow depending on which side of the screen. Then while the player keeps the mouse button pressed the avatar moves in that direction.

Ok, I think I get it.

Press and hold on the left side off the screen and you turn left as long as the button is down. You stop turning when the button is released. Top of the screen could move forward, bottom could move backward, etc.

If that is the case then when you get the MousePressed event start a timer that will generate a continuous stream of TimerEvents and step the motion based on those events. When you get the Mouse Released event kill the timer.

That keeps everything event-based. the alternative is if you have an active rendering loop then you don’t need the timer, you can just step you motion on each iteration of the game loop based on the state of the mouse button as recorded from events, or polled with JInput.

Hi to all,
I have implemented the thing i needed succesfully, I’d like to share the logic with you guys as well , so following is the code snippet of my application :-

public VisualizerCameraBehavior() {

WakeupOnAWTEvent wakeuponawtevent = new WakeupOnAWTEvent(33L);
WakeupOnAWTEvent wakeuponawtevent1 = new WakeupOnAWTEvent(16L);
WakeupOnAWTEvent wakeuponawtevent2 = new WakeupOnAWTEvent(8L);
WakeupOnElapsedFrames wakeuponelapsedframes = new WakeupOnElapsedFrames(0);
bothCondition = new WakeupOr(new WakeupCriterion[] {
                       wakeuponawtevent,   wakeuponawtevent1, wakeuponawtevent2, wakeuponelapsedframes
});
awtCondition = new WakeupOr(new WakeupCriterion[] {
                            wakeuponawtevent, wakeuponawtevent1, wakeuponawtevent2
});

}

public void processStimulus(Enumeration enumeration) {
while(enumeration.hasMoreElements())
{
WakeupCondition wakeupcondition = (WakeupCondition)enumeration.nextElement();
if(wakeupcondition instanceof WakeupOnAWTEvent)
{
processAWTEvents(((WakeupOnAWTEvent) wakeupcondition).getAWTEvent());
}
else if(wakeupcondition instanceof WakeupOnElapsedFrames)
{
/// do what you want to do. Move the camera on values calulated in processmouseevent() func
}
if(motion)
wakeupOn(bothCondition);
else
wakeupOn(awtCondition);
}

private void processAWTEvents(AWTEvent aawtevent[])
{
for(int i = 0; i < aawtevent.length; i++)
{
if(aawtevent[i] instanceof MouseEvent)
{
if((aawtevent[i].getID() == 506 || aawtevent[i].getID() == 501 || aawtevent[i].getID() == 502) /&& !ignoreMouseMotion/)
{
processMouseEvent((MouseEvent)aawtevent[i]);

}

}
}

protected void processMouseEvent(MouseEvent mouseevent)
{
float XValue = (float)mouseevent.getX();
float YValue = (float)mouseevent.getY();
if(SwingUtilities.isLeftMouseButton(mouseevent))
{
if(mouseevent.getID() == 502)
{
motion = false;
m_Movement = false;
} else
{
motion = true;
m_Movement = true;
// calculate and decide the direction of movement on basis of Xvalue and YValue

}
}

What are all those hardcoded numbers?? 501, 506, 502??

You don’t seriously code like that do you?

MouseEvent.MOUSE_PRESSED, MOUSE_RELEASED or MOUSE_DRAGGED (told you!) is too clear, you needed to obfuscate your code? :stuck_out_tongue:

Dear Sir,
If you Decompile the flybehavior.class, MouseBehaviour.class files of J3Dflythorugh then you will find the code in the same manner in those classes, I have just copied it from there in my mail with little bit editing, I hope my position is cleared now.
Take good care of your self

For what its worth,

The hardcoded numbers only come up if you decompile the source, when it was original coded they would have used symbols he mentions aboved. Decompilers just can’t do the mapping back…

Thanks for the code tho,

Kev

PS. Useful tip? When you post code to the forums you can use the [ code ] [ /code ] tags to format it.

EDIT: doh!

You didn’t initially say that you got that code from decompiling… in any case if you had followed the initial advice given you would have found the symbols that those constant represent and hopefully would learn the APIs better than taking the code from decompiled classes.

You do realize that you are effectively implementing a MouseMotionListener and processing MouseDragged events exactly as I originally suggested?

If you had taken the advice you would be 1 week ahead :slight_smile:

[quote]… following is the code snippet of my application
[/quote]
You mean that it was code you got by decompiling someone else’s application.