Skip to content

Commit

Permalink
[MouseImpulse] added new feature to apply like a collision with the m…
Browse files Browse the repository at this point in the history
…ouse
  • Loading branch information
EulalieCoevoet committed Oct 28, 2019
1 parent 8656e2c commit cf76690
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public LCPApp() {

public void setUp() {
system.mouseSpring = mouseSpring;
system.mouseImpulse = mouseImpulse;
systemDir = "datalcp/boatCraneSim.png";
loadSystem(systemDir);
T.getBackingMatrix().setIdentity();
Expand Down Expand Up @@ -145,7 +146,10 @@ public void display(GLAutoDrawable drawable) {
// draw a line from the mouse to the body point
Point2d tmp = new Point2d();
picked.transformB2W.transform( grabPointB, tmp );
gl.glColor4f( 1,0,0,0.5f);
if(mouseImpulse.isGrabbing())
gl.glColor4f( 1,0.5f,0,0.5f);
else
gl.glColor4f( 1,0,0,0.5f);
gl.glLineWidth( 5 );
gl.glBegin( GL.GL_LINES );
gl.glVertex2d( mousePoint.x, mousePoint.y );
Expand Down Expand Up @@ -237,10 +241,12 @@ public JPanel getControls() {

JTextArea ta = new JTextArea(
" left mouse drag - mouse spring\n" +
" middel mouse drag - translate scene ? \n" +
" shift + left mouse drag - mouse impulse\n" +
" middle mouse drag - translate scene ? \n" +
" right mouse drag - zoom in and out ?\n" +
" mouse wheel = zoom in and out ? \n" +
" space - start and stop sim\n" +
" space - start and stop simulation\n" +
" enter - start and stop recording\n" +
" m - simulate up to next merge or unmerge \n" +
" l - load a file \n" +
" left - previous scene\n" +
Expand Down Expand Up @@ -327,6 +333,7 @@ public void actionPerformed(ActionEvent e) {
vfp.add( system.getControls() );

vfp.add( mouseSpring.getControls() );
vfp.add( mouseImpulse.getControls() );

VerticalFlowPanel vfpv = new VerticalFlowPanel();
vfpv.setBorder( new TitledBorder("window content scaling controls") );
Expand Down Expand Up @@ -360,6 +367,7 @@ public void actionPerformed(ActionEvent e) {
private Point2d grabPointB = new Point2d();
private Point2d mousePoint = new Point2d();
private MouseSpringForce mouseSpring = new MouseSpringForce( mousePoint );
private MouseImpulse mouseImpulse = new MouseImpulse();

/**
* Loads the specified image, clearing the old system, and resets viewing parameters.
Expand Down Expand Up @@ -428,6 +436,8 @@ protected void systemClear() {
/** current index in the files list */
private int whichFile = 0;

private boolean shiftPressed = false;

/** Attaches mouse and keyboard listeners to the canvas. */
@Override
public void attach(Component component) {
Expand Down Expand Up @@ -460,7 +470,8 @@ public void mouseDragged(MouseEvent e) {

component.addMouseListener( new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
public void mousePressed(MouseEvent e) {

prevMousePoint.setLocation( e.getPoint() );
setPoint( e.getPoint().x, e.getPoint().y );
if ( (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0 ) {
Expand All @@ -469,16 +480,36 @@ public void mousePressed(MouseEvent e) {
picked.transformW2B.transform( mousePoint, grabPointB );
}
}
mouseSpring.setPicked( picked, grabPointB );

if(shiftPressed) {
if(!mouseImpulse.isGrabbing()) {
mouseImpulse.grab( picked, mousePoint );
}
} else {
mouseSpring.setPicked( picked, grabPointB );
}
}

@Override
public void mouseReleased(MouseEvent e) {
picked = null;
mouseSpring.setPicked( picked, grabPointB );
picked = null;
if(mouseImpulse.isGrabbing()) {
mouseImpulse.release( mousePoint );
} else {
mouseSpring.setPicked( picked, grabPointB );
}
}
} );

component.addKeyListener( new KeyAdapter() {

@Override
public void keyReleased(KeyEvent e) {
if ( e.getKeyCode() == KeyEvent.VK_SHIFT ) {
shiftPressed = false;
}
}

@Override
public void keyPressed(KeyEvent e) {
double ds = 0.5; // step size for moving controllable springs (might want to expose this as a parameter!)
Expand All @@ -491,6 +522,9 @@ public void keyPressed(KeyEvent e) {
run.setValue( ! run.getValue() );
}
}
else if ( e.getKeyCode() == KeyEvent.VK_SHIFT ) {
shiftPressed = true;
}
else if ( e.getKeyCode() == KeyEvent.VK_S ) {
double dt = stepsize.getValue() / (int)substeps.getValue();
for ( int i = 0; i < substeps.getValue(); i++ ) {
Expand All @@ -500,7 +534,6 @@ else if ( e.getKeyCode() == KeyEvent.VK_S ) {
stepped = true;
}
else if ( e.getKeyCode() == KeyEvent.VK_M ) {

system.triggerMergingEvent = true;
system.mergingEvent = false;
run.setValue( true );
Expand Down Expand Up @@ -585,31 +618,38 @@ else if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
if ( ss == substeps.getMinimum() ) return;
substeps.setValue( ss - 1 );
stepsize.setValue( stepsize.getValue() *(ss-1)/ss );
} else if ( e.getKeyCode() == KeyEvent.VK_1 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_1 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( -ds, 0, 0 );
}
} else if ( e.getKeyCode() == KeyEvent.VK_2 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_2 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( ds, 0, 0 );
}
} else if ( e.getKeyCode() == KeyEvent.VK_3 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_3 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( 0, -ds, 0 );
}
} else if ( e.getKeyCode() == KeyEvent.VK_4 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_4 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( 0, ds, 0 );
}
} else if ( e.getKeyCode() == KeyEvent.VK_5 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_5 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( 0, 0, -ds );
}
} else if ( e.getKeyCode() == KeyEvent.VK_6 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_6 ) {
for ( Spring s : system.controllableSprings ) {
s.moveWorldAttachmentAndRestLength( 0, 0, ds );
}
} else if ( e.getKeyCode() == KeyEvent.VK_7 ) {
}
else if ( e.getKeyCode() == KeyEvent.VK_7 ) {
for ( RigidBody b : system.bodies ) {

if (b instanceof RigidCollection) {
Expand All @@ -623,7 +663,7 @@ else if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
if(b.magneticBody)
b.activateMagnet = !b.activateMagnet;
}
}
}
}
} );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package mergingBodies;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

import mintools.parameters.DoubleParameter;
import mintools.swing.CollapsiblePanel;
import mintools.swing.VerticalFlowPanel;

public class MouseImpulse {

private RigidBody pickedBody = null;

private Point2d endPoint = new Point2d();

private Point2d pickedPoint = new Point2d();

public boolean released = false;

/**
* Creates a new mouse impulse, where the provided point will be updated with movement of the mouse
* @param pickedPoint
*/
public MouseImpulse() {
}

/**
* Sets the picked body and the point on that body
* @param picked
* @param point
*/
public void grab( RigidBody picked, Point2d point ) {
pickedBody = picked;
pickedPoint = new Point2d(point);
}

public void release( Point2d point ) {
released = true;
endPoint = new Point2d(point);
}

public boolean isGrabbing() {
return (pickedBody != null);
}

/**
* Applies the mouse impulse to the picked rigid body, or nothing if no body selected
*/
public void apply() {
if ( pickedBody == null || !released) return;

double distance = endPoint.distance( pickedPoint );

Vector2d force = new Vector2d();
Vector2d direction = new Vector2d();
direction.sub( pickedPoint, endPoint );
if ( direction.lengthSquared() < 1e-3 ) return;
direction.normalize();
force.scale( scale.getValue()*distance, direction );

if (pickedBody.isInCollection()) pickedBody.parent.applyForceW( pickedPoint, force );
pickedBody.applyForceW( pickedPoint, force );

// Apply only once
released = false;
pickedBody = null;
}

/**
* Mouse impulse scale
*/
public DoubleParameter scale = new DoubleParameter("mouse impulse scale", 100, 1, 1e4 );

/**
* @return controls for the collision processor
*/
public JPanel getControls() {
VerticalFlowPanel vfp = new VerticalFlowPanel();
vfp.setBorder( new TitledBorder("Mouse Impulse Controls") );
vfp.add( scale.getSliderControls(true) );
CollapsiblePanel cp = new CollapsiblePanel(vfp.getPanel());
cp.collapse();
return cp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,17 @@ public void apply() {
if ( direction.lengthSquared() < 1e-3 ) return;
direction.normalize();
force.scale( distance * k, direction );

if (picked.isInCollection()) picked.parent.applyForceW( grabPointW, force );
picked.applyForceW( grabPointW, force );

// spring damping forces
if (picked.isInCollection()) picked.parent.getSpatialVelocity( grabPointW, grabPointV );
else picked.getSpatialVelocity( grabPointW, grabPointV );
force.scale( - grabPointV.dot( direction ) * c, direction );


if (!picked.isInCollection()) {
picked.applyForceW( grabPointW, force );

// spring damping forces
picked.getSpatialVelocity( grabPointW, grabPointV );
force.scale( - grabPointV.dot( direction ) * c, direction );
picked.applyForceW( grabPointW, force );
}else {
//apply the spring force on the parent
picked.parent.applyForceW( grabPointW, force );
//also apply it on the child
picked.applyForceW( grabPointW, force );

// spring damping forces
picked.parent.getSpatialVelocity( grabPointW, grabPointV );
force.scale( - grabPointV.dot( direction ) * c, direction );
picked.parent.applyForceW( grabPointW, force );
picked.applyForceW( grabPointW, force );
}
if (picked.isInCollection()) picked.parent.applyForceW( grabPointW, force );
picked.applyForceW( grabPointW, force );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RigidBodySystem {
public CollisionProcessor collisionProcessor = new CollisionProcessor(bodies);

public MouseSpringForce mouseSpring;
public MouseImpulse mouseImpulse;

BooleanParameter useGravity = new BooleanParameter( "enable gravity", true );
DoubleParameter gravityAmount = new DoubleParameter( "gravitational constant", 1, -20, 20 );
Expand Down Expand Up @@ -122,6 +123,10 @@ public void advanceTime( double dt ) {
applySpringForces();
}

if (mouseImpulse != null) {
mouseImpulse.apply();
}

if (processCollisions.getValue()) {
collisionProcessor.collisionDetection(dt);
collisionProcessor.solveLCP(dt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void apply(double k, double c) {
force.scale( - scale, displacement );

body.applyForceW( pbw, force );
if ( body.parent != null ) {
if ( body.isInCollection() ) {
body.parent.applyForceW( pbw, force );
}
}
Expand Down

0 comments on commit cf76690

Please sign in to comment.