Sign in to follow this  
yesming

[J2ME]particle array collision question

Recommended Posts

yesming    122
/** *

Title:Bullets

*

Description: This Class used to create a group of random bullets and check collision with the plane. First Step,Create int[][] bulletsPos to recorded the info of the bullets. bulletsPos[i][0]----the type of the bullet. bulletsPos[i][1]----the X position of the bullet bulletsPos[i][2]----the Y position of the bullet Then,use initBulletsPos() to filled the info of bulletsPos[][]; newPos()---return a random bullets info. In the End,refreshBulletsPos(Sprite plane) to refresh the bullets Position,and check the collision. NOW THE QUESTION IS: Only a few bullets colliding with plane can be detect correct.The most of bullets can cross through the plane. I doubt the array,but i have no answer.Any reply will be thankful. *

Tony.Ming Email:Yesming@126.com

*

2004

* */ import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class Bullets { private static final int STOP=0; private static final int MOVE=1; private int TotalBullets=8;//The total number of bullets private int SPEED=2; private int collisionLength=10; private int ScreenWidth; private int ScreenHeight; private BulletSprite bulletSprite; private int[][] bulletsPos; private Random rdm; private boolean isInitPos=false; private Runtime rt; public Bullets(int ScreenWidth,int ScreenHeight) { System.out.println("begin to create Bullets...."); this.ScreenWidth=ScreenWidth; this.ScreenHeight=ScreenHeight; this.bulletImage=Img; //init random num rdm=new Random(); //init Bullets Positon this.bulletsPos=new int[this.TotalBullets][3]; this.initBulletsPos(); //init bullet sprite this.bulletSprite=new BulletSprite(Img,Img.getWidth(),Img.getHeight()); System.out.println("init Bullets...ok"); } private int getRNDLength(boolean LengthWidth) { //create a random num,the boolean control the range of the random num. int rnd; if(LengthWidth) rnd=(rdm.nextInt()&0x7fffffff)%this.ScreenWidth; else rnd=(rdm.nextInt()&0x7fffffff)%this.ScreenHeight; return rnd; } private int getRNDBulletType() { //create a random bullet type. int BulletType; int rnd=(rdm.nextInt()&0x7fffffff)%100; if(rnd<25) BulletType=BulletSprite.BULLET_TYPE_LEFT; else if(rnd<75) BulletType=BulletSprite.BULLET_TYPE_TOP; else BulletType=BulletSprite.BULLET_TYPE_RIGHT; return BulletType; } private void initBulletsPos() { for(int i=0;i<this.TotalBullets;i++) { int[] tmp=this.newBullets(); bulletsPos[i][0]=tmp[0]; bulletsPos[i][1]=tmp[1]; bulletsPos[i][2]=tmp[2]; } this.isInitPos=true; System.out.println("init bullets pos...ok!"); } private int[] newBullets() { //create a random bullet info. int[] Pos=new int[3]; Pos[0]=this.getRNDBulletType(); switch (Pos[0]) { case BulletSprite.BULLET_TYPE_LEFT: Pos[1]=-5; Pos[2]=this.getRNDLength(false); break; case BulletSprite.BULLET_TYPE_TOP: Pos[1]=this.getRNDLength(true); Pos[2]=-5; break; case BulletSprite.BULLET_TYPE_RIGHT: Pos[1]=this.ScreenWidth+5; Pos[2]=this.getRNDLength(false); break; } return Pos; } private boolean isCollision(Sprite plane,int x,int y,int r) { boolean result=false; int planeXCenter=plane.getX()+12; int planeYCenter=plane.getY()+12; int bulletXCenter=x+3; int bulletYCenter=y+3; if(Math.abs(planeXCenter-bulletXCenter)<r) { if(Math.abs(planeYCenter-bulletYCenter)<r) { result=true; } } return result; } public boolean refreshBulletsPos(Sprite plane) { //System.out.println("Beginto refresh Bullets Pos"+this.nowBullets); boolean result=false;//is|is NOT collison with the plane if(!this.isInitPos) this.initBulletsPos(); //refresh the bullets pos,and check the collision for(int i=0;i<this.TotalBullets;i++) { switch (bulletsPos[i][0]) { case BulletSprite.BULLET_TYPE_LEFT: bulletsPos[i][1]+=SPEED; bulletsPos[i][2]+=SPEED; if(bulletsPos[i][2]>this.ScreenHeight) { int[] tmp=this.newBullets(); bulletsPos[i][0]=tmp[0]; bulletsPos[i][1]=tmp[1]; bulletsPos[i][2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[i][1],bulletsPos[i][2],collisionLength); break; case BulletSprite.BULLET_TYPE_TOP: bulletsPos[i][2]+=SPEED; if(bulletsPos[i][2]>this.ScreenHeight) { int[] tmp=this.newBullets(); bulletsPos[i][0]=tmp[0]; bulletsPos[i][1]=tmp[1]; bulletsPos[i][2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[i][1],bulletsPos[i][2],collisionLength); break; case BulletSprite.BULLET_TYPE_RIGHT: bulletsPos[i][1]-=SPEED; bulletsPos[i][2]-=SPEED; if(bulletsPos[i][2]<0) { int[] tmp=this.newBullets(); bulletsPos[i][0]=tmp[0]; bulletsPos[i][1]=tmp[1]; bulletsPos[i][2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[i][1],bulletsPos[i][2],collisionLength); break; } } return result; } //paint the bullets public void paint(Graphics g) { for(int i=0;i<this.TotalBullets;i++) { this.bulletSprite.setPosition(this.bulletsPos[i][1],this.bulletsPos[i][2]); this.bulletSprite.paint(g); } } }

Share this post


Link to post
Share on other sites
fatjaba    122
I dont know if you have fixed this or not, but where is the plane? as in can you show the details of creating the plane please?
Im not sure but I have a feeling that when you update the position of the bullets and check for the collision you have moved it past the collision plane so it is not detected. However this is only after a brief look.

Share this post


Link to post
Share on other sites
McMcDonald    122
Hi!

Also i think that the bullets are too fast.

Solutions:
1) call the collision detection/movements multiple times each frame with some virtual timer.
for example 1 frame takes 100 ms, call your logic three times with virtual time 0,30,90.
2) use some line intersection algorithm, where the line is drawn from the last bullet position to the new bullet position.

Hope this helps!

McMc

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by fatjaba
I dont know if you have fixed this or not, but where is the plane? as in can you show the details of creating the plane please?
Im not sure but I have a feeling that when you update the position of the bullets and check for the collision you have moved it past the collision plane so it is not detected. However this is only after a brief look.


here are my plane class.

public class Plane extends Sprite
{
final public static int DIRECTION_UP=0;
final public static int DIRECTION_DOWN=1;
final public static int DIRECTION_LEFT=2;
final public static int DIRECTION_RIGHT=3;

public int speed=2;
public boolean isFreezed=false;

private int posX=0;
private int posY=0;
private int screenWidth=0;
private int screenHeight=0;
private Graphics g;

//width and height means the tank's size of each frame in the whole image.
public Plane(Graphics g,Image img,int width,int height,int screenWidth,int screenHeight)
{
super(img,width,height);
this.g=g;
this.screenWidth=screenWidth;
this.screenHeight=screenHeight;

this.posX=this.screenWidth/2-width/2;
this.posY=this.screenHeight-height*2;
}

public void moveTo(int direction)
{
if(!this.isFreezed)
{
switch (direction) {
case DIRECTION_UP:
this.posY = Math.max(this.posY - speed, 0);
this.setFrame(0);

//this.setTransform(this.TRANS_NONE);
break;
case DIRECTION_DOWN:
this.posY = Math.min(this.posY + speed,
this.screenHeight - this.getHeight());
this.setFrame(0);

// this.setTransform(this.TRANS_ROT180);
break;
case DIRECTION_LEFT:
this.posX = Math.max(this.posX - speed, 0);
this.setFrame(1);

//this.setTransform(this.TRANS_ROT270);
break;
case DIRECTION_RIGHT:
this.posX = Math.min(this.posX + speed,
this.screenWidth - this.getWidth());
this.setFrame(2);

//this.setTransform(this.TRANS_ROT90);
break;
}
}
}

public Sprite fireBullet(Image bullets)
{
Sprite bullet=new Sprite(bullets,4,9);
bullet.setFrame(0);
bullet.setPosition(this.posX+this.getWidth()/2,this.posY);
return bullet;
}


public void doMovement()
{
this.setPosition(this.posX,this.posY);
}

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by McMcDonald
Hi!

Also i think that the bullets are too fast.

Solutions:
1) call the collision detection/movements multiple times each frame with some virtual timer.
for example 1 frame takes 100 ms, call your logic three times with virtual time 0,30,90.
2) use some line intersection algorithm, where the line is drawn from the last bullet position to the new bullet position.

Hope this helps!

McMc


Maybe the probleam is not focus in the bullets speed.

Because if i changed the Totol number of bullets into ONE,it will be dectect correct each time.

Any way Thank u for your suggestion.i can used it in other games^_^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this