Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Hit Testing Irregular Shaped Objects in a Tiled Background
Started by keystagefun Jan 19 2019 10:41 AM

- - - - -
7 replies to this topic
hittest irregular curve car racing track pixel
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

keystagefun

[GLOBAL: userInfoPane.html]
keystagefun
  • Contributor

  • 334 posts
  • Corona SDK

Hi,

 

I wonder if anyone can help as I'm a bit stumped... revisiting something I shelved a while ago.

 

I'm developing a pretty simple top down racing game, but I need to be able to check if the car is on or off the track (to slow it down if it's off).

 

I have a static car in the middle of the screen and a moving tiled background behind.

 

The track has curves.

 

I'd considered trying to get the pixel colour values for a particular point "under" the car, which if the track was a specific colour would work. But that seems to be a.) impossible as the pixel value is for the whole screen, so I can't get what's underneath the car and b.) processor heavy to do with any frequency.

 

If anyone has any pointers or ideas on where I might start I'd be very grateful.

 

Thanks in advance.

 

Ian



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,585 posts
  • Corona SDK

Tip:  Never use the work simple (or any synonym in a post).  Game design isn't simple, thus your game isn't simple.  You're setting yourself up to think, "This should be easy."  Thus you set yourself up to be frustrated.

 

Your idea of sampling isn't bad, but may not be the best way to do it.  That said, it is possible.  Just sample ahead of the moving car like this:

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2019/01/SteeringCar_wTrackDetect.zip

(Post has a v1 folder, implying 'other version' but I decided not to make any more.  I had planned to make one showing a dual sample, but decided against it.)

 


  • StarCrunch likes this

[TOPIC: post.html]
#3

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 805 posts
  • Corona SDK

If you won't be changing your art very frequently, you could do some pre-processing: for each tile (the asset itself, not the piece of the map), trace a curve down the track's center and sample a few offsets into the tile (also the track width, if it varies), saving these in a list.

 

In the game itself, gather up the lists from any tile (on the map) you might see this frame, given your current position and speed. Using those offset / width combinations from the previous step, calculate the corresponding circles (mathematical ones, not necessarily display objects). If your car overlaps any of them, it's on the track, otherwise off.


  • roaminggamer likes this

[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 763 posts
  • Corona SDK

If you are using Tiled, then you could also consider assigning a type to each tile, e.g. "track" and "background", and simply keep checking what type of tile is underneath the car. This method would be far from pixel perfect though.


  • roaminggamer likes this

[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,585 posts
  • Corona SDK

@XeduR @Spyric,

 

I don't think he is using Tiled. UPDATE: He is!

 

He capitalized it in the title of the post, but did not mention the tool in the body. 

 

My guess is he either doesn't know about the tool or doesn't know that capitalizing the word tiled will immediately make us think about the tool.

 

I've always wished there were a 'agreed upon vocabulary' for game development, but though there are lists of terms to be found, most don't agree thus we often confuse each other with ambiguous statements.  :)

 

 

PS - That said, your suggestion is entirely solid.


Edited by roaminggamer, 19 January 2019 - 01:40 PM.

  • XeduR @Spyric likes this

[TOPIC: post.html]
#6

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 763 posts
  • Corona SDK

Yeah, there has been enough talk about Tiled on these forums in the past few months that as soon as I read "Tiled", my mind skipped there straight away. :D



[TOPIC: post.html]
#7

keystagefun

[GLOBAL: userInfoPane.html]
keystagefun
  • Contributor

  • 334 posts
  • Corona SDK

Thanks for the replies and sorry for the confusion... yes I am using Tiled. That will teach me to eat dinner in between writing the title and the body of the post...

 

But I don't *have* to use Tiled. I'm open to ideas basically, depending on what works from a workflow point of view and performance wise.

 

Assigning types to the tiles and checking what's under the car isn't going to work I don't think as it wouldn't be accurate enough on curved sections. I will give it a go though and see if the feel is right. Won't know unless I try. Possibly with a small tile size I could get away with it.

 

Mathematical approach... I like it and it can be very accurate. Level design more difficult than with Tiled is the drawback I guess. Artwork probably won't change often, but I'm hoping for quite a few tracks, so could be labour intensive up front.

 

Lots of food for thought and I'll check out the link from roaminggamer for the sampling ahead - thanks. When I said simple I clearly hadn't thought this part of it through!

 

Very much appreciate all the replies. Thank you.



[TOPIC: post.html]
#8

keystagefun

[GLOBAL: userInfoPane.html]
keystagefun
  • Contributor

  • 334 posts
  • Corona SDK

Just revisiting this as I've found a solution I think works quite nicely. Not pixel perfect, but similar to a simpler version of StarCrunch's suggestion about assigning offsets to each tile.

 

Using Tiled, I created my map.

 

I've then added some extra properties to each tile type as follows:

{
  id = 1,
  image = "tile-1.png",
  width = 50,
  height = 50,
  hitTest = {
      t="Off",
      topLeft={0,0},
      topRight={0.5,0},
      bottomRight={0.5,0.5},
      bottomLeft={0,0.5}},
  tileType = 1,
},

The key part is the hitTest part. This describes a smaller area of the tile that behaves differently from the rest of the tile.

 

The issue was that the majority of tiles contained either mostly track with a smaller section not track (like a corner piece) or the opposite (like an inverse corner piece).

 

So each tile has a default of either track (t="On") or not track (t="Off")

 

The coordinates (topLeft, topRight etc.) then describe a section of the tile that is the opposite of the rest of the tile, so if our vehicle is over that, the speed needs adjusting accordingly.

 

With one set of coordinates you can handle any shape that has a smaller rectangular section cut out of it, so a corner, edge piece, straight piece.

 

Next is to look at handling diagonals effectively and more complex shapes where it isn't just one rectangular section cut out of a square tile - ie t-shapes... I may not need that level of accuracy for what I'm doing though.

 

It works quickly, is pretty accurate and allows for easy editing of tracks using Tiled but goes beyond tiles just being "track" and "not track".

 

Thank you again for all the replies.


Edited by keystagefun, 25 January 2019 - 02:17 PM.



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: hittest, irregular, curve, car, racing, track, pixel