JAVA 2D滚动游戏布局和碰撞检测障碍 [英] JAVA 2D Scroll Game Layout and Obstacles with collision detection

查看:1036
本文介绍了JAVA 2D滚动游戏布局和碰撞检测障碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序一个好主意。我的大问题:
我在Java中的新手。我的Java在IT大学,但认为是绝对的基础!
通常我正在开发的.NET。

我做一个垂直滚动2D游戏:
闪屏(没有概率)
主菜单(没有概率)
层次概述(没有概率)
游戏本身(大概率:))

垂直滚动:我能做到这一点,足以教程
游戏地图和障碍(没办法)

如何创建一个地图(有墙左,右)
中间部分是免费的了障碍比赛。

有是(例如)在这里落下一个球,你必须把它以正确的方式之前,当顶部边框来球消失。

我如何可以检测从黑墙,红色障碍物发生碰撞?
(这就是一个例子,不包括纹理)

和不要忘记,它向下移动。我想用AndEngine带摄像头...

图片:


解决方案

  

我如何创建地图


在屏幕上或存储?


  • JOGL,LWJGL ..有太多的东西,你可以启动一个OpenGL屏幕上画图。(也许AndEngine是更好,我不知道),你需要使用OpenGL的兼容版本的功能。

  • 如果您需要在内存中生成墙上的信息,您可以使用一个高度地图superpositioned一个简单的函数。你需要计算表面法线才能够把事情别人之上。(林不知道,如果Android设备能够高分辨率实时性能)

例如,从一个正弦波,

添加最重要的是方波,

Superpositioned情况:

添加随机高度的地图,你会得到墙的一边与随机性,你可以只使用一个或两个code线(然后如果你需要更节省CPU周期比内存可以保存坐标生成它空间):


  

我如何检测碰撞


您是否需要基于事件或直接检测?


  • 先验检测:你拍的球虚光球的速度矢量方向。测试两个壁和红色的障碍坐标的光线交叉。如果相交,它就会知道哪些对象之后n个时间步球会发生碰撞。 (如果一切不变),你可以尝试多种射线的测试,工作背景,如第一次测试射击射线从当前位置,从球的下一个位置下一个测试射击,其次是下一个,...
    好:你会知道它之前碰撞,这样你就可以阻止它正是时候。您可以在这里计算的球会碰撞后再涨呢!即使连续碰撞。缺点:需要更多的CPU周期,递归可以杀死性能。不止一个移动的物体会增加复杂性指数。

使用计数排序拍摄之前光线一个单独的对象上的算法坐标缓冲器,可以使跟踪更快。


  • 后验检测,因为您检查只有当球在任何其他对象的坐标的公差区间协调配合很简单。
    良好:这是最简单的。缺点:它已经相撞。可能错误或爆炸,你需要把球回到一个合理的位置。如果物理的背后是很好的,你不会需要重新定位。
    该排斥力可以从截断指数潜力或一些简单的阶梯函数导出。

这两种类型需要一个加速结构,如果对象数量都很高。 (KD树帮助)


  • 您可以硬code中的碰撞带。每一个新的对象将增加code的另一条线的实施。需要最少的CPU周期。 (例如,检查碰撞地图,如果位置是墙壁的位置或任何其他障碍物,1碰撞,0无碰撞,图1和0的50x400一个不应填写太多内存)

I've got a great idea for an android app. My big problem: I'm a "beginner" in JAVA. I had Java at the IT university but that were absolute basics! Normally i'm developing in .Net .

I make a vertical scrolling 2D game: Splashscreen (no prob) Main menu (no prob) level overview (no prob) Game itself (big prob :) )

Vertical scrolling: I can do it, enough tutorials Game map and obstacles (no way)

How can I create a map (there are walls right and left) the middle part is free for playing with obstacles.

There is (for example) a ball where fall down and you have to turn it in the right way before the ball disappear when the top border comes.

How can i detect a collision from the black wall and the red obstacles? (thats an example, textures not included)

And not to forget, it moves down. I thought with AndEngine with camera...

Image:

解决方案

How can I create a map

On the screen or in the memory?

  • Jogl, lwjgl, .. there are too many things that you can start an opengl drawing on screen.(maybe AndEngine is better I dont know), you need to use version-compliant functions of opengl.
  • If you need to generate wall information in memory, you can use a simple function superpositioned with a height-map. You need to compute surface normals to be able to put things on top of others.(Im not sure if android device is capable of realtime performance for high resolution)

For example, starting with a sine wave,

Adding a square wave on top of that,

Superpositioned case:

Adding random-height-map, you get one side of wall with randomness and you can generate it using only one or two lines of code(then you can save coordinates if you need to save CPU cycles more than memory space):

How can i detect a collision

Do you need event-based or a direct detection?

  • Priori detection: you shoot imaginary rays from ball with direction of ball's velocity vector. Test both walls' and red obstacles' coordinates for the ray intersection. If it intersects, it will know which object the ball will collide after n time-steps. (if everything goes unchanged) You can try multiple ray test that works background such as first test shoots rays from current position, next test shoots from next position of ball, next is next, ... Good: you will know before it collides, so you can stop it "just in time". You can compute where the ball will be jumped after collision too! Even for consecutive collisions. Bad: needs more cpu cycles, recursion can kill performance. More than one moving objects will increase complexity exponentially.

Using "Counting Sort" algorithm on a separate object coordinate buffer before shooting rays, can make the tracing faster.

  • Posteriori detection is simple since you check for only if ball's coordinate fits in an tolerance interval of any other object's coordinate. Good: this is the simplest. Bad: it already collided. May bug or explode, you will need to put the ball back to a reasonable position. If the physics behind is good, you wont need repositioning. The repulsion force can be derived from a truncated exponential potential or some simple step function.

Both types need an acceleration structure if number of objects are high. (KD-tree helps)

  • You can hard-code the collision zones. Every new object will add another line of code for the implementation. Needs the least cpu cycles. (example,a collision map to check if a position is a wall-position or any other obstacle, 1 for collision and 0 for non-collision, a map of 50x400 of 1s and 0s should not fill too much memory)

这篇关于JAVA 2D滚动游戏布局和碰撞检测障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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