2d

Collision detection with bitmaps on SurfaceView's canvas in Android

放肆的年华 提交于 2019-12-03 22:15:56
In Android I use a SurfaceView to display a simple 2D game. The bitmaps (.png) with alpha (representing the game objects) are drawn on the canvas. Now I would like to do a simple but accurate collision detection. Checking whether these bitmaps are overlapping is quite easy. But how do I check for collisions when these bitmaps have transparent areas? My challenge is detecting whether two balls collide or not. They fill up the whole bitmap in width and height both but in all four edges, there are transparent areas of course as it's a circle in a square. What is the easiest way to detect

Pygame camera follow in a 2d tile game [duplicate]

陌路散爱 提交于 2019-12-03 20:49:38
This question already has an answer here: Add scrolling to a platformer in pygame 4 answers import pygame, sys from pygame.locals import * pygame.init() size = width, height = 480,320 screen = pygame.display.set_mode(size) r = 0 bif = pygame.image.load("map5.png") pygame.display.set_caption("Pygame 2D RPG !") x,y=0,0 movex, movey=0,0 character="boy.png" player=pygame.image.load(character).convert_alpha() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key==K_a: movex=-1 elif event.key==K_d: movex=+1 elif event

Adding the diagonal values in a 2d array

与世无争的帅哥 提交于 2019-12-03 20:31:32
I have the following 2d array int [][] array = {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}, {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}, {50, 51, 52, 53, 54, 55, 56, 57, 58, 59}, {60, 61, 62, 63, 64, 65, 66, 67, 68, 69}, {70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, {80, 81, 82, 83, 84, 85, 86, 87, 88, 89}, {90, 91, 92, 93, 94, 95, 96, 97, 98, 99}}; I have this code to find the sum of all the values in the array. How can I modify it to add only the diagonal values starting at 0 (0+11+22

Obtain ordered vertices of GeneralPath

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 18:59:04
How can I obtain the vertices of a GeneralPath object? It seems like this should be possible, since the path is constructed from points (lineTo, curveTo, etc). I'm trying to create a double[][] of point data (an array of x/y coordinates). You can get the points back from the PathIterator . I'm not sure what your constraints are, but if your shape always has just one closed subpath and has only straight edges (no curves) then the following will work: static double[][] getPoints(Path2D path) { List<double[]> pointList = new ArrayList<double[]>(); double[] coords = new double[6]; int numSubPaths

How to draw a Perspective-Correct Grid in 2D

谁都会走 提交于 2019-12-03 18:56:43
问题 I have an application that defines a real world rectangle on top of an image/photograph, of course in 2D it may not be a rectangle because you are looking at it from an angle. The problem is, say that the rectangle needs to have grid lines drawn on it, for example if it is 3x5 so I need to draw 2 lines from side 1 to side 3, and 4 lines from side 2 to side 4. As of right now I am breaking up each line into equidistant parts, to get the start and end point of all the grid lines. However the

Calculate a bezier spline to get from point to point

此生再无相见时 提交于 2019-12-03 17:20:07
问题 I have 2 points in X,Y + Rotation and I need to calculate a bezier spline (a collection of quadratic beziers) that connects these 2 points smoothly. (see pic) The point represents a unit in a game which can only rotate slowly. So to get from point A to B, it has to take a long path. The attached picture shows quite an exaggeratedly curvy path, but you get the idea. What formulas can I use to calculate such a bezier spline? 回答1: Just saw that I misunderstood your question. Couldn't you use a

How to build a Tiled map in Java for a 2D game?

怎甘沉沦 提交于 2019-12-03 17:12:09
Not sure how to approach this problem. Basically, I want a Pixel -> Tile representation of a 400x400 window. Each coordinate on the screen, e.g 120x300 should be part of a tile. My smallest sprite is 4 pixels, so we can say that 1 tile = 4 pixels. The player and enemy sprites are all 20 x 20, so each player/bad guy will occupy 5 tiles. Then I want to use this Map class to: Retrieve the x/y coordinates of a player/monster sprite by suppling the index/id of the tile. Knowing where the boundaries are, so it doesn't move the sprite beyond 400x400 , thus hiding it. Collision detection, knowing

Fast, Pixel Precision 2D Drawing API for Graphics App?

可紊 提交于 2019-12-03 17:02:19
I woud like to create a cross-platform drawing program. The one requirement for writing my app is that I have pixel level precision over the canvas. For instance, I want to write my own line drawing algorithm rather than rely on someone elses. I do not want any form of anti-aliasing (again, pixel level control is required.) I would like the users interactions on the screen to be quick and responsive (pending my ability to write fast algorithms.) Ideally, I would like to write this in Python, or perhaps Java as a second choice. The ability to easily make the final app cross-platform is a must.

How to pass 2-D vector to a function in C++?

十年热恋 提交于 2019-12-03 17:01:52
问题 If it is passed, is it passed by value or by reference? void printMatrix(vector<vector<int>> *matrix); ... vector<vector<int>> matrix(3, vector<int>(3,0)); printMatrix(&matrix1); 回答1: Since your function declaration: void printMatrix(vector< vector<int> > *matrix) specifies a pointer, it is essentially passed by reference. However, in C++, it's better to avoid pointers and pass a reference directly: void printMatrix(vector< vector<int> > &matrix) and printMatrix(matrix1); // Function call

Unprojecting 2D Screen Coordinates to 3D Coordinates

六眼飞鱼酱① 提交于 2019-12-03 16:17:34
I was wondering how I would go about mapping 2D screen coordinates to a 3D world (specifically the xz plane) knowing: -position of the camera -equation of the screen plane -equation of the xz plane All I want to do is have the land on the xz plane light up when I hover the mouse over it. Any help is greatly appreciated! Thanks! If your world is rotated and shifted such that the camera is at x=y=z=0 (world coordinates), the world z coordinate increases away from the viewer (into the screen), and the projection plane is parallel to the screen and is at z=d (world coordinate; d > 0 ), then you