# How to get a 2D vectorial terrain from an image for Java collision detection?

I'm trying to make a 2D Java-only game with a terrain like the one shown below. In the previous games I made, I used tile based collsions, but now, I want to use vector based collsions, as an uneven terrain would require minuscule tiles.

What I'd like to know, is how I can get the vectors from the map above, and how I should use them.

For the first part, I could manually measure out each of the coordinates, as shown below, but especially for lager and more detailed maps, that would be a tremendous amount of work.

I was wondering if there is a way to do this easier. I've seen tools that let you draw the vectors over the image, but those export only to engines like Box2D and Unity, which are programmed in different code languages. As I stated before, I only want to use Java.

For the second part, if I have the coordinates of the vectors, what should I do with them? I previously used rectangles, but now I would need to use either lines or polygons that connect each coordinate.

Also, how do I check for collisions between the player's polygon or rectangle, and the polygons or lines that form the terrain? When using rectangles, I could use playerR.Intersects(terrainR), but how can I do this with a terrain?

A link to an easy-to-understand tutorial would be a nice answer too, especially for the last part of my question.

• I'm sorry if this is considered a "dumb" question, I'm quite new to game development. I've already searched on google for a good tutorial, but I couldn't find one. I even tried Yahoo, who knew that one still existed :-) – Jaïr Paalman Apr 13 '17 at 22:42

I want to use vector based collsions, as an uneven terrain would require minuscule tiles

This is a good approach. Don't mix display and logic: keep a detailed map (pixel-based) for display, and a vector version for logic.

Most games, even 3d ones, have these two versions: a collision mesh, and a display mesh.

What I'd like to know, is how I can get the vectors from the map above, and how I should use them. For the first part, I could manually measure out each of the coordinates, as shown below, but especially for lager and more detailed maps, that would be a tremendous amount of work.

You're correct. AAA games can pay their artists to make the collision mesh of all their assets, or pay to develop a tool to do this for them (mesh decimation). But those solutions are expensive/a waste of time for the hobbyist game developper.

I was wondering if there is a way to do this easier.

There are image-analysis algorithms (skeletton extraction, etc). Those are fidgety, hard to control and hard to code. There would be few available, and fewer in Java. They are usually scientific-minded, I don't think it is very suitable for a game development.

I suggest you work the other way around: generate the vector level by hand and let the game display that with augmented precision by selecting tiles that agree with the vector logic. This has the following advantages:

• Less hand work. In design phase, you'll decide high-level object placement, not fiddle with low-level graphics. Easier to define a generic rectangle-shaped room than take the brush and draw it out.
• Easier automation. It's easier to complexify a geometry (introduce displacement, noise, tile variations) than to simplify it (in which case you'd have to be careful of not obstructing small passages, etc.)
• Easier maintenance. If you chose to change your aesthetics, no need to redefine the whole graphics-->Vector process. You just swap the tileset, and regenerate.

if I have the coordinates of the vectors, what should I do with them? I previously used rectangles, but now I would need to use either lines or polygons that connect each coordinate.

Also, how do I check for collisions between the player's polygon or rectangle, and the polygons or lines that form the terrain? When using rectangles, I could use playerR.Intersects(terrainR), but how can I do this with a terrain?

In general, a lot of resources exist, even on SO, like this one.

If your characters follow gravity, a few raycasts downwards would do the job very nicely. You'll simply have to have a line-line intersection routine, and get the closest.