# Distance between two points within a polygon (or raster value) in R

I'm trying to get the shortest distance between each point in a a set of coordinates (lat/long) within a body of water. However, straight line distances don't work because the points represent the movement of an animal, and the body of water is curved. Thus, straight line distance results in impossible movements overland.

Is there anyway to get the distances within the boundaries of the polygon (shapefile)? Or is there a function that will calculate distance within a specified value of a raster (or ignoring NA) such that I can set the entire body of water to one value, the land to another, and it will calculate the distance only within the value for the water?

I have quite a few data sets that I need to do this for, so I really need a way to quickly process them automatically.

What function would work would this?

This is the same problem that was described here, but I am trying to do it in R not QGIS Calculating shortest distance between points within polygon using QGIS?
{"ft_lang_label":"__label__en","ft_lang_prob":0.96015626,"math_prob":0.9423735,"size":985,"snap":"2021-31-2021-39","text_gpt3_token_len":205,"char_repetition_ratio":0.13149847,"word_repetition_ratio":0.0,"special_character_ratio":0.20203045,"punctuation_ratio":0.078125,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9821681,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-27T13:05:29Z\",\"WARC-Record-ID\":\"<urn:uuid:c3ba1d83-678d-4414-9d2f-a818daf35cf9>\",\"Content-Length\":\"158793\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e5c09657-e72b-44f7-89d9-f0165d60508f>\",\"WARC-Concurrent-To\":\"<urn:uuid:380911de-2027-437f-a49b-397ace919354>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://gis.stackexchange.com/questions/371670/distance-between-two-points-within-a-polygon-or-raster-value-in-r?noredirect=1\",\"WARC-Payload-Digest\":\"sha1:QDJN45PBO2U3VC5ZY5N5G3MYPD5UMY7F\",\"WARC-Block-Digest\":\"sha1:K42CDDFHLHTRAVRJ6HRIYDFZIMN4MCLF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153391.5_warc_CC-MAIN-20210727103626-20210727133626-00428.warc.gz\"}"}