2d

Efficient algorithm for collisions in 2D game?

我们两清 提交于 2019-12-04 09:50:21
I'm programming a Bomberman in Java following a tutorial (this is my first game). The tutorial suggests the following code for detecting collisions. for (int p=0; p<entities.size(); p++) { for (int s=p+1; s<entities.size(); s++) { Entity me = (Entity) entities.get(p); Entity him = (Entity) entities.get(s); if (me.collidesWith(him)) { me.collidedWith(him); him.collidedWith(me); } } By now, entities is an array list containing the enemies and the player. As I want to also detect the player collides with walls, should I put every single wall or bricks tile in the level into the entities arraylist

Point inside 2D axis aligned rectangle, no branches

泪湿孤枕 提交于 2019-12-04 09:04:46
I'm searching for the most optimized method to detect whether a point is inside an axis aligned rectangle. The easiest solution needs 4 branches (if) which is bad for performance. Given a segment [x0, x1] , a point x is inside the segment when (x0 - x) * (x1 - x) <= 0 . In two dimensions case, you need to do it twice, so it requires two conditionals. Consider BITWISE-ANDing the values of XMin-X, X-XMax, YMin-Y, Y-YMax and use the resulting sign bit. Will work with both ints and floats. I think you will need the four tests no matter what, but if you know if the point is more likely to be in or

2D orbital physics

那年仲夏 提交于 2019-12-04 08:25:07
问题 I'm working on a 2D physics engine for a game. I have gravity and masses working, using a simple iterative approach (that I know I'll have to upgrade eventually); I can push the masses around manually and watch them move and it all works as I'd expect. Right now I'm trying to set up the game world in advance with a satellite in a simple circular orbit around a planet. To do this I need to calculate the initial velocity vector of the satellite given the mass of the planet and the desired

Shortest route between multiple points

二次信任 提交于 2019-12-04 07:35:14
问题 I need to find the shortest route between multipe points. Let's say that I have these four points: var startPoint = new Point(1, 1); var pointsToGoPast = new List<Point> { new Point(3,1); new Point(2,4); }; var endPoint = new Point(10, 10); So, I want to find out which points to go past first, in order to get the shortest route, from startPoint to endPoint. Can anyone help me? Update: It has to go past each of the points in the pointsToGoPast list. The cost is even for each route. 回答1: You

How to create a curve between 2 points in 2D and get back Points that makes that curve every d distance?

笑着哭i 提交于 2019-12-04 07:18:31
I'm not good in math. I have 2 points, A(x1, y1) and B(x2, y2) in 2D. I need to create a virtual path from point A to B curved at R(radius), and then return an array of points which are describing this curved path, not all maybe every D(distance) from each other. In Java I need a method like this: private ArrayList<PointF> generateCurve(PointF pFrom,PointF pTo,float pRadius,float pMinDistance){ ArrayList<PointF> pOutPut = new ArrayList<PointF>(); // ...generate result to pOutPut return pOutPut; } How to do this ? Martijn Courteaux I didn't gave up and I've been working on it for a few more

How to resize an image without loading into memory?

不打扰是莪最后的温柔 提交于 2019-12-04 07:04:35
I'd like to check the dimensions of an image, and then size it down if the width exceeds a given threshold. When I load a 6MB JPEG image with ImageIO.read(), the BufferedImage allocates about 45MB of heap space. Is there a way to do a passthrough image resize without loading all the data into memory? I tried passing ImageIO.read() a File object, thinking it would stream the data from disk, but it doesn't help. Check out im4java and JMagick . Both use an extremely efficient external tool called ImageMagick for image manipulation. Im4Java calls the command-line app (does work in separate process

Optimal covering with non-uniform discs

点点圈 提交于 2019-12-04 06:32:10
What kind of algorithm can I use to search for an optimal (minimum area) covering of a limited region of the XY plane with n discs ( x j , y j , r j ) ? I've found many investigations on fixed radius discs, but nothing about variable radius. n is fixed but the discs can be placed freely (they're not in assigned positions and their centers are not required to be inside the region). The region is in general non-connected and non-simply connected (can be composed by multiple parts and can have holes). In my specific case is defined by multiple closed polygons (using odd-even filling rule). To

Creating a dynamic 2D matrix in Java

两盒软妹~` 提交于 2019-12-04 06:24:25
问题 I want a dynamic matrix, number rows and columns unkonw, filling it by clicking on a button. Bu there is more: I don't want to add entire rows, but just one cell at the time, one click = one cell added. Of course not randomly : 1st cell of 1st row, 2nd cell of 1st row... and then the same of the 2nd row and so one... I know about UJMP, ArrayList, but it's not quite what I'm looking for. Please be accurate on your answer, thank you in advance. 回答1: Use this: List<List<Integer>> dynamicMatrix =

Java 2D Collision?

怎甘沉沦 提交于 2019-12-04 06:18:13
问题 Hey guys i'm making a 2D java game and i'm trying to figure out how to make a good collision code. I am currently using the following code: public void checkCollision() { Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32); for(Wall wall : walls) { Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32); if (player_rectangle.intersects(wall_rectangle)) { Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle); if

How to load images from json object on canvas image?

情到浓时终转凉″ 提交于 2019-12-04 05:49:56
问题 Need to plot alphaball.png image on different positions on canvas image. Positions (x and y co-ordinates) need to pass from data json object. with the code mention alphaball.png is not visible. what can be done? //JSON object data = [{ "x": ["200"], "y": ["200"] }, { "x": ["150"], "y": ["150"] }, { "x": ["300"], "y": ["300"] } ]; window.onload = function(){ var canvas = document.getElementById("myCanvas"); var context = canvas.getContext('2d'); var dataJSON = data || [] var files = ["gkhead