Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Heretic86 on September 11, 2013, 07:03:43 pm

Title: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 11, 2013, 07:03:43 pm
I figure this is a Script that has already been written, but it is probably buried within another script.  Since a method like this would most likely be used as part of a larger script, this is why I figure it would be buried.  Being that it most likely has already been written, Im not calling it a Script Request, but a Find That Script post.  Well, unless you want to consider it a script request.

What I am looking for is to get map coordinates returned as an array between two points.  Two events, event and player, player and new position, whatever, but two points on the Map.  Each point on the map will have @x and @y Map coordinates, So I'd need something like this:

Lets do a Right Triangle with matching sides.

Point A is at Map Coordinates @x = 10 and @y = 10
Point B is at Map Coordinates @x = 7 and @y = 13

array I would need back would look something like this for the Coordinates not at the location, just in between:
return [ [9,11],[8,12] ]

I've forgotten too much of my Trig...
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: KK20 on September 11, 2013, 11:05:01 pm
The real problem in question here is how you would want it to work when the two coordinates aren't perfectly 1:1 linear. What would you want to return when the two locations are (1,2) and (4,9)?

The most logical way to do this would be to


This isn't much, if any, trigonometry. I'm curious how you "forgotten too much" of trigonometry yet are a programmer.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: G_G on September 12, 2013, 12:24:45 am
Easy, coming from someone who's only taken a semester of highschool trig and programs quite a few things lol. I just tend to google my math problems if I have an issue, I've probably learned more from Google than my one class in highschool.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Blizzard on September 12, 2013, 01:46:32 am
You can use Math.hypot for that. Just throwing that out there.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: KK20 on September 12, 2013, 04:40:05 pm
How...would you use that exactly? :???:
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 12, 2013, 05:33:59 pm
Quote from: KK20 on September 11, 2013, 11:05:01 pm
The real problem in question here is how you would want it to work when the two coordinates aren't perfectly 1:1 linear. What would you want to return when the two locations are (1,2) and (4,9)?

The most logical way to do this would be to

  • find the differences between the two coordinates (so using my above example, that'd just be 3 and 7)

  • get a ratio so that it is less than 1 (3/7 = about 0.429)

  • choose one of the locations and add/subtract this ratio to @x or @y, whichever one was the numerator (in this example, @x) and in/decrease the other by 1 (which would be @y). If have any decimal numbers, round to the nearest whole number. Push this location.

  • repeat increasing and decreasing @x and @y until you reach or exceed your second location



This isn't much, if any, trigonometry. I'm curious how you "forgotten too much" of trigonometry yet are a programmer.


Good point.

Um, lets go with "get_coords(char_a, char_b)" for now.

Ratio:  I guess we could go with rounding.  Once I can play around with points, I'll see if it works well enough or not.

I never actually took trig, or any other programming class of any form.  I found out one of my friends died last night as well.  He was barely into his 30's.  Makes me think Im way old now.  I suspect it had something to do with heavy drug use earlier in his life.  It sucks a lot, but thats why I didnt reply yesterday.

@Blizz: math.hypot?  :P

Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: LiTTleDRAgo on September 12, 2013, 09:24:02 pm
@heretics : something like this?


class Game_Character
 def distance_from(character)
    Math.hypot(@x - character.x, @y - character.y)
 end
end


Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 12, 2013, 09:46:40 pm
Something like that, but that just returns the distance between two events.

I need something that will return the Map Coordinates along the hypot, not the Distance, so I can check Terrain Tags for each of those Tags.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: LiTTleDRAgo on September 12, 2013, 10:50:20 pm
like this?
---

Note : untested
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 13, 2013, 08:35:45 am
I appreciate the effort but it didnt work.  This is what I finally came up with.

Spoiler: ShowHide

class Game_Character
 #--------------------------------------------------------------------------
 # * Map XY Between
 #   target : game_character (player or event)
 #
 #   This will return an Array of Map XY Coordinates between a Target
 #   Character 1 is at Map Coordinates x=5 and y=7
 #   Character 2 is at Map Coordinates x=2 and y=7
 #   Array Returned will be all Map Coordinates between
 #   return [[3,7],[4,7],[5,7]]
 #--------------------------------------------------------------------------
 def map_xy_between(target)
   # Distance between this Character and Target
   y = (target.real_y - @real_y) * 1.0
   x = (target.real_x - @real_x) * 1.0
   # Holds Map X and Y coordinates: [[1,2],[2,3],[3,4]]
   map=[]
   # If there is enough space to have a Map Coordiante in between
   if x.abs + y.abs > 128
     # If this Character and Target have Same X
     if @real_x == target.real_x
       # Start at This Event, Increment toward Target
       offset = (@real_y < target.real_y) ? 128 : -128
       # While the Distance does not include each Map Tile between
       while y.abs > offset.abs
         map << [@x, ((@real_y+offset)/128).round]
         # Increment for each Map Tile
         offset += (@real_y < target.real_y) ? 128 : -128
       end
     # If this Character and Target have Same Y
     elsif @real_y == target.real_y
       # Start at This Event, Increment toward Target
       offset = (@real_x < target.real_x) ? 128 : -128
       # While the Distance does not include each Map Tile between
       while x.abs > offset.abs
         map << [((@real_x+offset)/128).round, @y]
         # Increment for each Map Tile
         offset += (@real_x < target.real_x) ? 128 : -128
       end
     # X and Y differ. We need Trig to determine each Map X Y Coordinate
     else
       # Use Larger Distance to calculate in Increments of 128
       if x.abs > y.abs
         # Tangent
         tan = x/y
         # Start at One Tile away
         x_offset = (x < 0) ? -128 : 128
         while x_offset.abs < x.abs
           # Determine Length of Side using Trig
           y_offset = x_offset / tan
           # Generate Map Coordinates based on Real Positions
           map_x = (@real_x/128).round + (x_offset/128).round
           map_y = (@real_y/128).round + (y_offset/128).round
           # Add Array to Map of Stored Coordinates betwen Target
           map << [map_x, map_y]
           # Increments of 128 for One Tile for each Real Map Coordinate
           x_offset = (x > 0) ? x_offset + 128 : x_offset - 128
         end          
       else
         # Tangent
         tan = y/x
         # Start at One Tile away
         y_offset = (y < 0) ? -128 : 128
         while y_offset.abs < y.abs
           # Determine Length of Side using Trig
           x_offset = y_offset / tan
           # Generate Map Coordinates based on Real Positions
           map_x = (@real_x/128).round + (x_offset/128).round
           map_y = (@real_y/128).round + (y_offset/128).round
           # Add Array to Map of Stored Coordinates betwen Target
           map << [map_x, map_y]
           # Increments of 128 for Real Map Coordinates
           y_offset = (y > 0) ? y_offset + 128 : y_offset - 128
         end
       end
     end
   end
   # Return any collected Map Coordinates between Event and Target
   return map
 end
end


Code is not perfect and not useful by itself.  I am thinking it will be useful in several scenarios, first one being my Move Straight script.  In that script, when an Event moves straight from one location to another location in a straight line, it doesnt check ANY Map Passage settings to determine if it can pass through them or not.  This did the trick for getting the General Passability settings on the map to be checked.  This version requires a Target Event, but that should be easy enough for other scripters to just plug in map coordinates instead of a Target Event.  

What it does currently is draws a straight line between self and target. Increments by 128 for real_x and real_y for one Map Coordinate between self and target per 128 vertical or horizontal.  The array that is returned can be used for various things, mostly for checking if a Map Coordinate is passable or not.  The goal was to just get it working so it probably needs a bit of cleanup.  I figure someone might find it useful.

Any suggestions on improving it that wont obfuscate it into oblivion?
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: ThallionDarkshine on September 13, 2013, 03:46:28 pm
I'm thinking that what you should probably try is the Bresenham's line algorithm (http://en.wikipedia.org/wiki/Bresenham's_line_algorithm). It probably would work pretty well for that purpose.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 13, 2013, 06:06:30 pm
Sounds like a challenge and probably worth doing just for the sake of doing.  And might even run better.

+Rep!
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: KK20 on September 13, 2013, 06:20:48 pm
So after reading that Bresenham line algorithm (didn't know this actually had a name :|), it sounds exactly like what I first posted.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: ThallionDarkshine on September 21, 2013, 03:17:41 pm
Yeah, basically.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 22, 2013, 08:37:20 am
Well, I shutter to think about the performance issues in comparison to that algorythm.

As far as Trig, heh, well that was easy.  Picked it up in about an hour on YouTube.  And our schools claim they need six months to teach you that?  No wonder our education system is in the slumps and spits out products like me!
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: KK20 on September 23, 2013, 11:42:05 am
The performance might not actually be all that bad. It's just some simple calculations, mainly adding and subtracting. It's only a O(n) run time, which is pretty fast. Hell, if I have time, I'll do it for you.

And yeah, trig is no big deal. It's calculus that starts weeding out the true math people. ;)
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: ThallionDarkshine on September 23, 2013, 03:41:01 pm
Well calculus isn't even that bad either. My friend basically taught me derivatives in about 45 minutes.
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: KK20 on September 23, 2013, 03:52:36 pm
If calculus was only about derivatives, I wouldn't have to drop out of the class my senior year in high school. Integration is where it's at. You also have to consider that the highest level of math most people probably get up to is Algebra 2. Calculus to them is alien talk (or Hell they have to trek over for a year and run away from math entirely after that).

-jumps back on topic-
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Blizzard on September 23, 2013, 04:28:35 pm
Derivatives are easy. You just have to learn a few transformations and that's it. Learning integration is not simply a reverse process, it also often requires that you can "see" how the integrals are supposed to be split into fragments which can then be derived easily.
The whole thing becomes easier, though, once you realize that integration is nothing but a continuous "sum" or the "summed area" under a line within a Cartesian coordinate system. (e.g. when integrating x, you get x2/2 which is exactly the area under the line with the formula y = x)

Spoiler: ShowHide
(http://kotanikazuhiko.files.wordpress.com/2011/04/figure102028229.png)


Now back on topic for real, lol!
Title: Re: Are there any Scripts that return Map Coordinates along a Hypotenuse?
Post by: Heretic86 on September 25, 2013, 09:11:32 am
Guess im not a true Math person!  It all seems like Magic to me, does that make me Non-Mathemagical?