有没有办法用 Java 处理检查一个方法是否绘制了另一种方法? [英] Is there a way to check if one method draws over another method with Java Processing?

查看:45
本文介绍了有没有办法用 Java 处理检查一个方法是否绘制了另一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本的游戏,需要在 drawObstacle 和 drawRocketShip 方法之间实现碰撞检测.我需要检查一种方法是否创建了与另一种方法重叠的图像.由于我采用了一些随机化和位置相似性检查,为了简洁起见,这些检查没有被分成不同的类.如果需要,我可以进一步详细说明,但我认为不一定需要开始.

I'm creating a rudimentary game and need to implement collision detection between the drawObstacle and drawRocketShip methods. I need to check if one method creates an image that overlaps another method. These aren't separated into different classes because of some randomization and position similarity checks I employ that have been omitted for the sake of brevity. I can elaborate further if required, but I thought that it wasn't necessarily needed to begin with.

public class SideScrollGame extends PApplet {

// Variables determining motion, window size, etc.

    public float rock1;
    public float rock2;
    public float rock3;
    public float rock4;
    public float rock5;


    public void setup(){
        size(width,height);
        //rock1 to rock5 are randomized values determining height.
    }

    public void draw(){
        moveShip();
        moveRock();

        drawObstacle(rock1, 3);
        drawObstacle(rock2, 3);
        drawObstacle(rock3, 3);
        drawObstacle(rock4, 3);
        drawObstacle(rock5, 3);         

        translate(x,y);
        // Code behind motion omitted for sake of brevity. It just moves the 
        // ship up and down across the Processing applet
        drawRocketShip();
        // drawObstacle and drawRocketShip create composite images using the
        // ellipse(), rect(), and triangle() functions.

    }
}

推荐答案

您也许可以使用 get() 函数来做到这一点.您可以在 参考 中找到更多信息,但基本上它允许您在特定像素.您可以基于此执行一些逻辑.

You could maybe do it that way using the get() function. You can find more info in the reference, but basically it allows you to get the color at a specific pixel. You could perform some logic based on that.

但老实说,这不是你应该考虑的方式.与其将碰撞检测基于实际绘制的内容,不如将碰撞检测基于存储在内存中的一组形状.在大多数情况下,一个简单的圆形或矩形就可以了.

But honestly, that's not the way you should be thinking about this. Instead of basing your collision detection on what's actually drawn, you should base your collision detection on a set of shapes that you store in memory. For most cases, a simple circle or rectangle will work just fine.

如果您使用 translate() 函数绘制所有内容,这会变得更加复杂,但您可以使用 screenX() 将坐标转换为屏幕坐标,并且screenY() 函数.同样,更多信息可以在参考中找到.

This gets more complicated if you're using the translate() function to draw everything, but you can convert your coordinates into screen coordinates using the screenX() and screenY() functions. Again, more info can be found in the reference.

无耻的自我推销:我在Processing available 这里写了一个关于碰撞检测的教程,但互联网上有大量资源.

Shameless self-promotion: I've written a tutorial on collision detection in Processing available here, but there are a ton of resources on the internet.

如果您仍然遇到问题,请将您的问题缩小到 MCVE,我们将从那里着手.祝你好运.

If you're still having trouble, please narrow your problem down to a MCVE and we'll go from there. Good luck.

这篇关于有没有办法用 Java 处理检查一个方法是否绘制了另一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆