简体   繁体   中英

I need some sort of rigid body collision detection engine for Android

I have tried to make my own to work on Android, but I have failed. What I mean with rigid body collision detection is that basically there is a wall and as soon as the player touches the wall, it cannot go through. I have tried to use Box2D, Emini Engine, PPhys2D, Phys2D, and either they dont have enough tutorials or they are really complicated. I wrote this to use with my normal Java games:

//package
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DCDE {
int plx, ply, obx, oby, obw, obh, plw, plh;
Rectangle north;
Rectangle east;
Rectangle south;
Rectangle west;
boolean debug=true;
public void debug(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;
    if(debug)
    {
        if(!(north==null)&&!(east==null)&&!(south==null)&&!(west==null)){
            g2.setColor(Color.YELLOW);
            g2.draw(north);
            g2.draw(east);
            g2.draw(south);
            g2.draw(west);
            g2.fill(north);
            g2.fill(east);
            g2.fill(south);
            g2.fill(west);
        }
    }
}
public void detect(int plxj, int plyj, int obxj, int obyj, int obwj, int obhj, int plwj, int plhj)
{
    plx=plxj;
    ply=plyj;
    obx=obxj;
    oby=obyj;
    obw=obwj;
    obh=obhj;
    plw=plwj;
    plh=plhj;
    Rectangle playr = new Rectangle(plx, ply, plw, plh);
    Rectangle objr = new Rectangle(obx, oby, obw, obh);
    north = new Rectangle((obx), (oby-1), obw, 1);
    east = new Rectangle((obx+obw), (oby), 1, obh);
    south = new Rectangle((obx), (oby+obh)+1, obw, 1);
    west = new Rectangle((obx-1), oby, 1, obh);
    if(playr.intersects(north)){
        ply=(oby-plh-1);
        if(debug)System.out.println("NORTH");
    }
    if(playr.intersects(east)){
        plx=(obx+obw+1);
        if(debug)System.out.println("EAST");
    }
    if(playr.intersects(south)){
        ply=(oby+obh+1);
        if(debug)System.out.println("SOUTH");
    }
    if(playr.intersects(west)){
        plx=(obx-plw-1);
        if(debug)System.out.println("WEST");
    }
}
}

This worked fine on the computer-java games i had made, but now I need something like the above but for android. So is there some sort of engine JUST made for collision detection and not the whole gravity stuff, or is there some easy maths way I could use?

Thanks in advance.

Why not just use a graphics engine that already works on android, like Unity or AndEngine . I'm they've already have thing kind of stuff built in along with a bunch of other nice things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM