I'm creating a scenario which when I click the world, a magnetic field appears and Points are attracted to it. Besides this, the points are repelled from my cursor wherever it is within the Greenfoot window. I am having trouble writing the code for this. Can anyone be of assistance?
I use:
And:
import greenfoot.Greenfoot;
/**
* A 2D vector. The vector can be read and manipulated in Cartesian coordinates
* (as an x,y-offset pair) or in polar coordinates (as a direction and a length).
*
* @author Poul Henriksen
* @author Michael Kolling
*
* @version 2.0
*/
public final class Vector
{
double dx;
double dy;
int direction;
double length;
/**
* Create a new, neutral vector.
*/
public Vector()
{
}
/**
* Create a vector with given direction and length. The direction should be in
* the range [0..359], where 0 is EAST, and degrees increase clockwise.
*/
public Vector(int direction, double length)
{
this.length = length;
this.direction = direction;
updateCartesian();
}
/**
* Create a vector by specifying the x and y offsets from start to end points.
*/
public Vector(double dx, double dy)
{
this.dx = dx;
this.dy = dy;
updatePolar();
}
/**
* Set the direction of this vector, leaving the length intact.
*/
public void setDirection(int direction)
{
this.direction = direction;
updateCartesian();
}
/**
* Add another vector to this vector.
*/
public void add(Vector other)
{
dx += other.dx;
dy += other.dy;
updatePolar();
}
/**
* Set the length of this vector, leaving the direction intact.
*/
public void setLength(double length)
{
this.length = length;
updateCartesian();
}
/**
* Scale this vector up (factor greater than 1) or down (factor less than 1).
* The direction remains unchanged.
*/
public void scale(double factor)
{
length = length * factor;
updateCartesian();
}
/**
* Set this vector to the neutral vector (length 0).
*/
public void setNeutral() {
dx = 0.0;
dy = 0.0;
length = 0.0;
direction = 0;
}
/**
* Revert to horizontal component of this movement vector.
*/
public void revertHorizontal() {
dx = -dx;
updatePolar();
}
/**
* Revert to vertical component of this movement vector.
*/
public void revertVertical() {
dy = -dy;
updatePolar();
}
/**
* Return the x offset of this vector (start to end point).
*/
public double getX() {
return dx;
}
/**
* Return the y offset of this vector (start to end point).
*/
public double getY() {
return dy;
}
/**
* Return the direction of this vector (in degrees). 0 is EAST.
*/
public int getDirection() {
return direction;
}
/**
* Return the length of this vector.
*/
public double getLength() {
return length;
}
/**
* Update the direction and length from the current dx, dy.
*/
private void updatePolar()
{
this.direction = (int) Math.toDegrees(Math.atan2(dy, dx));
this.length = Math.sqrt(dx*dx+dy*dy);
}
/**
* Update dx and dy from the current direction and length.
*/
private void updateCartesian()
{
dx = length * Math.cos(Math.toRadians(direction));
dy = length * Math.sin(Math.toRadians(direction));
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A variation of an actor that maintains precise location (using doubles for the co-ordinates
* instead of ints). It also maintains a current movement in form of a movement vector.
*
* @author Poul Henriksen
* @author Michael Kolling
*
* @version 2.1
*/
public abstract class SmoothMover extends Actor
{
private Vector movement;
private double exactX;
private double exactY;
public SmoothMover()
{
this(new Vector());
}
/**
* Create new thing initialised with given speed.
*/
public SmoothMover(Vector movement)
{
this.movement = movement;
}
/**
* Move in the current movement direction.
*/
public void move()
{
exactX = exactX + movement.getX();
exactY = exactY + movement.getY();
super.setLocation((int) exactX, (int) exactY);
}
public boolean atWorldEdge()
{
return exactX >= getWorld().getWidth() || exactX < 0
|| exactY >= getWorld().getHeight() || exactY < 0;
}
/**
* Set the location using exact (double) co-ordinates.
*/
public void setLocation(double x, double y)
{
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);
}
/**
* Set the location of this actor. Redefinition of the standard Greenfoot
* method to make sure the exact co-ordinates are updated in sync.
*/
public void setLocation(int x, int y)
{
exactX = x;
exactY = y;
super.setLocation(x, y);
}
/**
* Return the exact x co-ordinate (as a double).
*/
public double getExactX()
{
return exactX;
}
/**
* Return the exact y co-ordinate (as a double).
*/
public double getExactY()
{
return exactY;
}
/**
* Modify the current movement by adding a new vector to the existing movement.
*/
public void addForce(Vector force)
{
movement.add(force);
}
/**
* Accelerate the speed of this mover by the given factor. (Factors less than 1 will
* decelerate.) The direction remains unchanged.
*/
public void accelerate(double factor)
{
movement.scale(factor);
if (movement.getLength() < 0.15) {
movement.setNeutral();
}
}
/**
* Return the speed of this actor.
*/
public double getSpeed()
{
return movement.getLength();
}
/**
* Return the current movement of this object (as a vector).
*/
public Vector getMovement()
{
return movement;
}
}
