0
\$\begingroup\$

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.

enter image description here

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.

enter image description here

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.

Thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ 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 :-) \$\endgroup\$ – Jaïr Paalman Apr 13 '17 at 22:42
1
\$\begingroup\$

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.

A link to an easy-to-understand tutorial would be a nice answer

See this Gamasutra article for a full-fledged collision detection algo. There are no easy solutions to player-mesh collision in the general case.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. It will be hard for me to work the other way around and generate an image from the vector map, though, as I'm not making a tile based game. If you want to know what I'm trying to achieve, search on Google images from "Spell Sword". \$\endgroup\$ – Jaïr Paalman Apr 14 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.