Hit Detection Pseudocode Optimization

Hey there fellow scripters that know a f*ckton more than i do about programming.

I'm looking for some help optimizing some code in my current project regarding hit detection. I don't need exact codes at the moment so theoretical pseudocode is all I need. So first off, things you need to know:

  • I'm using pixel movement, checking the X and Y of a player will effectively give you decimal point numbers in multiples of 0.125.

So at the moment, the mechanic I have in place does this:

The player can hit(trigger) an event that has one tile between it and the player, if the player is facing the event. The player can be off by 1 tile horizontally to the direction he is facing, so there is a leeway of space where the event will trigger.

The pseudocode for it is as follows:

If A button is being pressed

 - If player is within tile distance (by ints) of an event. Parameters are distance and if facing the event

 - - When player is facing down, check if player y is less than event y, and if player x is within 1 of event x

 - - When player is facing left, check if player x is more than event x, and if player y is within 1 of event y

 - - When player is facing right, -- - - - - - - etc

 - - When player is facing up, -- - - - - - - etc

 - else false

I'm wondering if this is the most efficient way to check for hit detection. at specific distances, changing the parameters in green when needed.
Can you draw it? Sounds like you're saying the player can trigger an event they're facing if there's a blank tile between them (like you'd normally get with a shopkeeper if there's a counter tile in between). Is that it?


What about if there's NO tile between (player is immediately in front of the event)? If player is on/under a tile that has a different priority? What about events that the player is NOT facing (player would not be able to trigger them, but can THEY trigger if they're set to event touch, and do they have to be within a tile or could they have a tile between them and the player)? What about a Player Touch event if no button is being pressed (and what is the A button btw? The normal action button is the C button, isn't it?


And what coordinates are you checking? Screen, Real, or x/y based on map? Are you using the same set of coordinates for both the player and the event?
This is probably the best reference for the hit detection. It's an early build, fully evented before being converted into a script. Before, the check would happen within the events parallel process, which wasn't as efficient as just checking events when the player presses a button:




The event must be in front of the player within a distance of 1 tile or less, so 1 tile in between or right in front. If the player is slightly past the event, as in on the same tile or further, aka not facing the event, it will not hit. For the sake of simplicity I won't be checking whether something is between the event and the player.

The hit detection isn't through event touch, its a parallel process that checks distance whenever a button is pressed, whatever button I want to designate.

The coordinates I'm using are $game_player.y and $game_player.x, along with $game_map.events[#].y and $game_map.events[#].x
If you're using $game_player.x/y and $game_map.events[id].x/y it's not really pixel collision then. So you could just use the same method Game_Player uses to check if there's an event on the tile in front of the player. Look at the check_event_there method and see if you can grab the line(s) that grab events right in front of the player.
its not pixel collision, I only mention pixel movement because I can check distances between the player and the event in increments of 0.125, or 1/8ths of a tile.

So essentially, I'm looking for a more efficient way of what I'm doing right now, which checks distance and facing direction when the player presses a button. The event doesn't have to be right in front of the player nor should they be, I can modify it to be 5.625 tiles away from the player and still trigger.

Original thread: https://forums.rpgmakerweb.com/threads/hit-detection-pseudocode-optimization.18354/