I would like to have ramps in my Megaman EX game, but I have no idea how to make it with my physics engine in mind. Could someone please help me?
Here's my Physics Engine, Matter.class (the player is a subclass of Matter):
import greenfoot.*;
public class Matter extends SpriteSheet
{
public void shadowImagery(boolean s){}
/**Copy everything between this blue comment and the next one to apply
physics to a class
Made by Entity1037
*/
int xmove=0;
int subymove=0;
int subxmove=0;
int ymove=0;
int gravityTimeRateMax = 1; //Amount of cycles until speed increases
int gravityTimeRate = gravityTimeRateMax;
int frictionAmountMax = 2; //How many cycles until the object slows
//down due to friction
int xFrictionAmount=0;
int yFrictoinAmount=0;
String gravityDirection = "down";
boolean gravity = true; //Just in case you want to switch gravity ;D
Class[] objects = {Base.class,SymbolBlock.class,/*Spike.class,SpikeSheet.class,*/BossDoor.class,Box.class,Ramp1.class,null}; //All objects that can be collided with
public void gravity(boolean g){gravity=g;}
public void gravityDirection(String d){gravityDirection = d;}
boolean onGround = false;
int xca = 0; //How much xmove changes (Example: "xmove=xca" where "xca=-xmove")
int yca = 0; //How much ymove changes (Example: "ymove=yca" where "ycs=-ymove")
/**
This method must be called every cycle before "physics()" is called!!!
Protip: place it in the "act()" method!
Protip#2: If you would like, you can just call this method at the beginning of "physics()".-
-This will make it the same speed altering values apply to all matter subclasses
*/
public void setSpeedAlteringValues(int X, int Y){xca=X; yca=Y;} //Lets you set speed altering values
int x=getImage().getWidth()/2; //Half of the width of the hitbox
int y=getImage().getHeight()/2; //Half of the height of the hitbox
public void setHitBoxHalfValues(int X, int Y){x=X; y=Y;} //Lets you set hit box dimensions
public int getHitBoxXHalfValue(){return x;} //Allows you to obtain hit box values of actors in "Matter"
public int getHitBoxYHalfValue(){return y;} //Allows you to obtain hit box values of actors in "Matter"
int xSpeedLimit=9;//This is self explanitory (caps how fast subclasses of matter can travel on the X axis)
int ySpeedLimit=7;//Again, self explanitory
public void setSpeedLimits(int xl, int yl){xSpeedLimit=xl; ySpeedLimit=yl;} //Lets you set the speed limit
boolean particals=false;
boolean wasGrounded=false;
String collide = "";
public void particals(boolean p){particals=p;}
Actor collidedActor;
boolean movehold=false;
public void physics(){
collidedActor=null;
wasGrounded=onGround;
int collisionAmount=0;
boolean xhold=false;
boolean yhold=false;
boolean ground=false;
GreenfootImage image = new GreenfootImage(getImage());
//Collision detection
collide="";
while (collisionAmount<objects.length){
if (objects[collisionAmount]==null)break;
int a;
//Down check
a=y;
while (ymove>=0){
for (int i=-x+3; i<x-3; i++){
Actor object = getOneObjectAtOffset(i, a+ymove,objects[collisionAmount]);
if (object!=null){
if (ymove!=0)collide="down";
if (ymove!=0){
yhold=true; ymove=yca;
setLocation(getX(),object.getY()-object.getImage().getHeight()/2-y);
}
if (gravityDirection.equals("down")){ground=true; collidedActor=object;}
image=new GreenfootImage(object.getImage());
break;
}
}
if (ymove==0||a>=ymove+y){break;}else{a++;}
}
//Up check
a=-y;
while (ymove<=0){
for (int i=-x+3; i<x-3; i++){
Actor object = getOneObjectAtOffset(i, a+ymove-1,objects[collisionAmount]);
if (object!=null){
if (ymove!=0)collide="up";
if (ymove!=0){
yhold=true; ymove=yca;
setLocation(getX(),object.getY()+object.getImage().getHeight()/2+y);
}
if (gravityDirection.equals("up")){ground=true; collidedActor=object;}
image=new GreenfootImage(object.getImage());
break;
}
}
if (ymove==0||a<=ymove-y){break;}else{a--;}
}
//Left check
a=-x;
while (xmove<=0){
for (int i=-y+3; i<y-3; i++){
Actor object = getOneObjectAtOffset(a+xmove-1, i,objects[collisionAmount]);
if (object!=null){
if (xmove!=0)collide="left";
if (xmove!=0){
xhold=true; xmove=xca;
setLocation(object.getX()+object.getImage().getWidth()/2+x,getY());
}
if (gravityDirection.equals("left")){ground=true; collidedActor=object;}
image=new GreenfootImage(object.getImage());
break;
}
}
if (xmove==0||a<=xmove-x){break;}else{a--;}
}
//Right check
a=x;
while (xmove>=0){
for (int i=-y+3; i<y-3; i++){
Actor object = getOneObjectAtOffset(a+xmove, i,objects[collisionAmount]);
if (object!=null){
if (xmove!=0)collide="right";
if (xmove!=0){
xhold=true; xmove=xca;
setLocation(object.getX()-object.getImage().getWidth()/2-x,getY());
}
if (gravityDirection.equals("right")){ground=true; collidedActor=object;}
image=new GreenfootImage(object.getImage());
break;
}
}
if (xmove==0||a>=xmove+x){break;}else{a++;}
}
collisionAmount++;
}
//Gravity
if (ground==false&&gravity==true){
if (gravityTimeRate==0){
gravityTimeRate=gravityTimeRateMax;
if (gravityDirection.equals("down"))subymove++;
if (gravityDirection.equals("up"))subymove--;
if (gravityDirection.equals("left"))subxmove--;
if (gravityDirection.equals("right"))subxmove++;
}else{
gravityTimeRate--;
}
}else{gravityTimeRate=gravityTimeRateMax;}
onGround=ground;
image.scale(1,1);
//Friction
/**if (ground==true&&xFrictionAmount<frictionAmountMax){
xFrictionAmount++;
}
if (xFrictionAmount==frictionAmountMax&&onGround){
xFrictionAmount=0;
if (gravityDirection.equals("up")||gravityDirection.equals("down")){
if (xmove>0)subxmove--;
if (xmove<0)subxmove++;
}
if (gravityDirection.equals("left")||gravityDirection.equals("right")){
if (ymove>0)subymove--;
if (ymove<0)subymove++;
}
}*/
//Sub-move to move conversion
int m=1;
if (subxmove<0){subxmove=m; xmove--;}
if (subxmove>m){subxmove=0; xmove++;}
if (subymove<0){subymove=m; ymove--;}
if (subymove>m){subymove=0; ymove++;}
//Speed Limit
if (xmove>xSpeedLimit)xmove=xSpeedLimit;
if (xmove<-xSpeedLimit)xmove=-xSpeedLimit;
if (ymove>ySpeedLimit)ymove=ySpeedLimit;
if (ymove<-ySpeedLimit)ymove=-ySpeedLimit;
//Move commands
if (movehold==false){
if (xhold==false)setLocation(getX()+xmove,getY());
if (yhold==false)setLocation(getX(),getY()+ymove);
}
//Partical effects
if (particals==true){
int p=ymove;
if (p<0)p=-p;
if (p==0)p=1;
if (collide.equals("down")){
for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(7)-3,Greenfoot.getRandomNumber(p)-p-1,image),getX()+Greenfoot.getRandomNumber(getImage().getWidth())-getImage().getWidth()/2,getY()+getImage().getHeight()/2);}
}
if (collide.equals("up")){
for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(7)-3,Greenfoot.getRandomNumber(p)+1,image),getX()+Greenfoot.getRandomNumber(getImage().getWidth())-getImage().getWidth()/2,getY()-getImage().getHeight()/2);}
}
p=xmove;
if (p<0)p=-p;
if (p==0)p=1;
if (collide.equals("left")){
for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(p)+1,Greenfoot.getRandomNumber(4),image),getX()-getImage().getWidth()/2,getY()+Greenfoot.getRandomNumber(getImage().getHeight())-getImage().getHeight()/2);}
}
if (collide.equals("right")){
for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(p)-p-1,Greenfoot.getRandomNumber(7)-3,image),getX()+getImage().getWidth()/2,getY()+Greenfoot.getRandomNumber(getImage().getHeight())-getImage().getHeight()/2);}
}
}
}
/**End of code (don't forget to call the "physics" method)!*/
/**
* Hit box collision detection code (just in case you have no idea
how to write your own collision detection code)
P.S.: I do realize that any hit box bigger than the object's
image will not regi
*/
public void detectCollision(){
Actor matter = getOneIntersectingObject(Matter.class);
if (matter!=null){
int XX = ((Matter)matter).getHitBoxXHalfValue();
int XY = ((Matter)matter).getHitBoxYHalfValue();
for (int w=matter.getX()-XX; w<=matter.getX()+XX; w++){
for (int h=matter.getY()-XY; h<=matter.getY()+XY; h++){
if (w>getX()-x&&w<getX()+x&&h>getY()-y&&h<getY()+y){
//Insert code here
}
}
}
}
}
}

