简体   繁体   中英

Java & Slick2d - Object Interaction

In a game I am creating with slick2d to learn Java, I have multiple levels each with one Footballer and multiple other units I want the footballer to be able to interact with. I also want the other units to interact with each other (for example multiple Ball s colliding with eachother) (note, some of these units are sometimes of the same class as one another e/g multiple Defender s). I am unsure, however how to detect these interactions and update the units appropriately. For example, I have my Footballer :

public class Footballer extends Unit {
    public Footballer(float x, float y){
        super("res/ballerpicture", x, y)
        }
    }

And within this class I have an update function which overrides the update function in the Unit class (allowing me to move the one Footballer according to my input - this is working without issue other than collision detection).

I could then, for example, have loaded on my map 5 balls:

public class Ball extends Unit {
    public Ball(float x, float y){
        super("res/ballpicture", x, y)
        }
    }

For an example, I would like to know how to update any one of the balls upon collision with the Footballer , moving them one tile away from the player each time they collide.

My Unit class includes a move method which moves the unit based on an integer direction (left = 1, right = 2 etc...).

Apologies if I have oversaturated this question or haven't included enough information - I'm relatively new to java.

What you are looking for is collision detection . All objects which are able to interact with each other can have a hitbox, that's in the easiest way one geometrical shape which represents the body of an object. We could for example assume that your ball has a circle as hitbox with the radius of 8 px and your footballer has a hitbox of a rectangle with width 32 px and height 32 px.

When both objects are now moving you have to check whether the boundaries of your hitbox are intersecting with each other, if so: do something, if not keep moving.

In Slick2D all shapes have a method called intersects(Shape s), it returns true if the boundaries of two shapes are intersecting. So basically you just have to implement hitboxes for your objects (make sure to update the hitbox when your object is moving) and then check for intersecting. There are a lot of different ways of implementing collision detection and the internet is providing a lot of ressourcing regarding that topic. I also recommend to take a look at the Shape documentation of Slick2D. It's hard to write a solution for you since I don't know your code but I'm sure you will figure it out and Slick2D provides an easy preimplemented solution for your problem with the intersecting method.

It could look somewhat like the following:

Edit, for multiple balls:

//in your update method
for(Ball ball : allBalls){
if(footballer.getHitbox().intersects(ball.getHitbox()){
//get direction of footballer
ball.move(...)
}
}

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