我在 2D Java 游戏中遇到碰撞检测问题。
通常,我会为可能与其他对象发生冲突的对象创建一个 getBounds() 方法。此方法将返回 a new Rectangle(x,y,width,height),其中x和y是精灵左上角的坐标,width和height是精灵的宽度和高度。
但是在我目前正在开发的游戏中,有一个由用户控制的“坦克”。只要玩家按住向左或向右箭头按钮之一,该坦克的精灵就会旋转。换句话说,它可以旋转到任何角度。坦克的精灵是一个矩形。
所以我不能简单地做我在这种情况下一直做的事情。
如何检测与这种精灵的碰撞?谢谢
我需要检查是否有Polygon另一个实例相交Polygon.(该intersects()方法不会这样做,因为它只接受一个Rectangle2D或一个矩形区域作为争论).
如果重要的话,那两个Polygons我想要检查它们是否相交,都是长方形的,但是以不是90度的天使旋转,所以我不能使用这种intersects()方法,或者至少我认为我可以'吨.
我怎样才能做到这一点?谢谢
我正在尝试将JPanel转换为BufferedImage,然后将该图像保存到文件中.
DrawingPanel是一个扩展JPanel的类.
public class J extends JFrame {
public J(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
DrawingPanel panel = new DrawingPanel();
add(panel);
BufferedImage bi = (BufferedImage) panel.createImage(500,500);
File file = new File("file.png");
setVisible(true);
try {
ImageIO.write(bi, "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:IllegalArgumentException: image == null!
此错误发生在这一行:
ImageIO.write(bi, "PNG", file);
Run Code Online (Sandbox Code Playgroud)
问题是什么?谢谢
编辑:DrawingPanel类:
public class DrawingPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
setBackground(Color.BLACK);
}
}
Run Code Online (Sandbox Code Playgroud) #include <fstream>
#include <iostream>
using namespace std;
class file_reader {
public:
file_reader(const char* file_name) : file(ifstream(file_name)) { }
char operator[](int index) const {
file.seekg(index);
return file.get();
}
private:
ifstream file;
};
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:'std::ios_base::ios_base(const std::ios_base&)' is private.好像代码试图在层次结构中调用更高的构造函数.虽然ifstream有一个构造函数,需要一个const char*.有什么问题?
我继承std::list了允许[]运算符'伪随机访问' .
#include <list>
template <typename T>
class rlist : public std::list<T> {
T& operator[](int index){
typename std::list<T>::iterator iterator;
int pos;
for (iterator = this->begin(), pos = 0; iterator != this->end(); iterator++, pos++)
if (pos == index)
return *iterator;
return inexistent_element();
}
class inexistent_element {
};
};
Run Code Online (Sandbox Code Playgroud)
inexistent_element还没有继承T,所以这不应该编译.但它编译.另外我很确定C++不应该允许我通过非const引用传递一个内联创建的对象.
我正在使用Code :: Blocks IDE和MinGW gcc编译器.我想知道为什么这会编译.
这是我的代码:
template <typename container_type>
void transfer(container_type container, iterator begin, iterator end) {
for (; begin != end; begin++)
if (!element_in_container(container, *begin))
container.insert(iterator, *begin);
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误'iterator is not a type'.
我尝试添加std::或container_type::之前iterator,没有帮助.我尝试将模板定义为template <typename container_type<typename T> >和迭代器一样container_type<T>::iterator,没有运气.怎么了?
java ×4
c++ ×3
bounding-box ×1
collision ×1
inheritance ×1
intersection ×1
oop ×1
polygon ×1
sprite ×1
swing ×1
templates ×1