Class PhysixObject
- java.lang.Object
-
- greenfoot.Actor
-
- PhysixObject
-
public abstract class PhysixObject extends greenfoot.Actor
The core of the physix game engine. It represents ao object with a mass and a velocity. The physix class runs the physics of the movement as well as the collision physics between physixObjecs and StaticObjects. It is abstract ifself and is meant to be extended. Collision physics are still WIP.- Version:
- 1.0
- Author:
- RcCookie, Poul Henriksen (Smooth moving), Michael Kolling (Smooth moving), Neil Brown (Smooth moving)
-
-
Field Summary
Fields Modifier and Type Field Description protected twoD.Vector
velocity
This Vector represents the speed moved at the end of the last frame.
-
Constructor Summary
Constructors Constructor Description PhysixObject(double weight)
Creates a new PhysixObject with the given weight.PhysixObject(double weight, double surfaceFriction, boolean usingGravity, boolean usingWind)
Creates a new PhysixObject with the given weight and friction and sets gravity and wind to the given booleans.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
act()
First runs the run method which represents the action code in the extending classes.void
addAcc(twoD.Vector acceleration)
Adds acceleration to the object.void
addForce(twoD.Vector force)
Adds acceleration to the object.boolean
forwards()
Checks weather the object is moving forwards compared to its rotation.twoD.Vector
getForce()
Return the force that this object carries at the moment.twoD.Point
getLocation()
Returns the location of the objectdouble
getMass()
Returns the objects mass.double
getSpeed()
Returns the absolute movement speed in the last frame.twoD.Vector
getVelocity()
Returns the velocity Vector, meaning the last frames movement.int
getX()
Returns the x coordinate of the objectint
getY()
Returns the y coordinate of the objectvoid
move(double distance)
Moves the object by the given exact distance.void
move(int distance)
Moves the object by the given distance.void
move(twoD.Vector v)
Moves the object by the given Vector.abstract void
run()
This method should contain all stuff that usually would be in the act method.void
setCw(double cw)
Adjusts the cw-value of this object.void
setFriction(double f)
Sets the friction the object has on the ground.static void
setGravity(double g)
Adjusts the gravity to a new acceleration value.void
setLocation(double x, double y)
Sets the object to a new location.void
setLocation(int x, int y)
Sets the object to a new exact location.void
setLocation(twoD.Point location)
Sets the object to a new location given through a point.void
setMass(double m)
Gives the object a new mass.void
setSpeed(twoD.Vector speed)
Sets the speed of the object to the given Vector.void
useGravity(boolean b)
Sets the gravity to be active or not active for this object.void
useWind(boolean b)
Sets the wind resistance to be active or not active for this object.-
Methods inherited from class greenfoot.Actor
addedToWorld, getImage, getIntersectingObjects, getNeighbours, getObjectsAtOffset, getObjectsInRange, getOneIntersectingObject, getOneObjectAtOffset, getRotation, getWorld, getWorldOfType, intersects, isAtEdge, isTouching, removeTouching, setImage, setImage, setRotation, turn, turnTowards
-
-
-
-
Constructor Detail
-
PhysixObject
public PhysixObject(double weight)
Creates a new PhysixObject with the given weight. The object will have the following default values: friction = 0.1, useGravity = false, useWind = true.- Parameters:
weight
- The objects weight
-
PhysixObject
public PhysixObject(double weight, double surfaceFriction, boolean usingGravity, boolean usingWind)
Creates a new PhysixObject with the given weight and friction and sets gravity and wind to the given booleans.- Parameters:
weight
- The objects weightsurfaceFriction
- The objects frictionusingGravity
- Weather the object should be affected by gravityusingWind
- Weather the object should be affected by wind resistance
-
-
Method Detail
-
act
public final void act()
First runs the run method which represents the action code in the extending classes. Then it runs gravity, wind and friction resistance, moves the object to its new location using colissions and updates the velocity Vector. This method is final because overriding it would remove any physics calculations. The run method is meant to be overridden as the act method normaly.- Overrides:
act
in classgreenfoot.Actor
- See Also:
run()
-
run
public abstract void run()
This method should contain all stuff that usually would be in the act method. It is called once per frame by the act method. This method has to be overridden in the extending classes, even if it stays empty. Usually though it contains for examle user input actions. This method is abstract mainly to be noticed by the user as he is used to overriding the act method which is not possible.
-
addForce
public void addForce(twoD.Vector force)
Adds acceleration to the object. The acceleration is determined by the force input and the objects weight.- Parameters:
force
- The force Vector to add
-
addAcc
public void addAcc(twoD.Vector acceleration)
Adds acceleration to the object. The acceleration is not determined by the weight of the object. For that reason this method should be used carefully and mainly finds use in gravity physics as mass does not play a role here. Usually though addForce should be used.- Parameters:
acceleration
- The acceleration Vector to add
-
setSpeed
public void setSpeed(twoD.Vector speed)
Sets the speed of the object to the given Vector. Should mainly be used to stop rather than to move objects.- Parameters:
speed
- The new speed
-
setMass
public void setMass(double m)
Gives the object a new mass. The mass will be corrected if neccessary to be greater than 0 to prevent any glitches. Warning: If the mass is less than 1, forces very quickly become very powerful.- Parameters:
m
- The new mass
-
setFriction
public void setFriction(double f)
Sets the friction the object has on the ground. It will be corrected if neccessary to fit the allowed range between 0 and 1. 0 means no friction.- Parameters:
f
- The new friction
-
useGravity
public void useGravity(boolean b)
Sets the gravity to be active or not active for this object.- Parameters:
b
- Weather gravity is active or not
-
setGravity
public static void setGravity(double g)
Adjusts the gravity to a new acceleration value. If gravity is greater than 0, objects will be faling upwards. The gravity value will be used for all objects that use gravity.- Parameters:
g
- The new gravity acceleration
-
useWind
public void useWind(boolean b)
Sets the wind resistance to be active or not active for this object.- Parameters:
b
- Weather wind resistance is active or not
-
setCw
public void setCw(double cw)
Adjusts the cw-value of this object. It will be corrected if neccessary to be greater or equal to 0.- Parameters:
cw
- the new cw value
-
forwards
public boolean forwards()
Checks weather the object is moving forwards compared to its rotation. Can be helpful for braking mechanisms.
-
move
public void move(int distance)
Moves the object by the given distance.- Overrides:
move
in classgreenfoot.Actor
- Parameters:
distance
- The distance to move
-
move
public void move(double distance)
Moves the object by the given exact distance.- Parameters:
distance
- The exact distance to move
-
move
public void move(twoD.Vector v)
Moves the object by the given Vector.- Parameters:
v
- The Vector to move
-
setLocation
public void setLocation(double x, double y)
Sets the object to a new location.- Parameters:
x
- The new x coordinatey
- The new y coordinate
-
setLocation
public void setLocation(int x, int y)
Sets the object to a new exact location.- Overrides:
setLocation
in classgreenfoot.Actor
- Parameters:
x
- The new exact x coordinatey
- The new exact y coordinate
-
setLocation
public void setLocation(twoD.Point location)
Sets the object to a new location given through a point.- Parameters:
location
- The location to set
-
getLocation
public twoD.Point getLocation()
Returns the location of the object- Returns:
- The objects location
-
getX
public int getX()
Returns the x coordinate of the object- Overrides:
getX
in classgreenfoot.Actor
- Returns:
- The obejcts x coordinate
-
getY
public int getY()
Returns the y coordinate of the object- Overrides:
getY
in classgreenfoot.Actor
- Returns:
- The obejcts y coordinate
-
getVelocity
public twoD.Vector getVelocity()
Returns the velocity Vector, meaning the last frames movement.- Returns:
- The velocity Vector
-
getSpeed
public double getSpeed()
Returns the absolute movement speed in the last frame.- Returns:
- The obejcts movement speed
-
getForce
public twoD.Vector getForce()
Return the force that this object carries at the moment.- Returns:
- The objects force
-
getMass
public double getMass()
Returns the objects mass.- Returns:
- The objects mass
-
-