skellt

skellt

Apple Game Frameworks and Technologies: Distance calc could be simpler for this point of the book (p 54)

The very top line of page 52 is let distance = hypot(pos.x-player.position.x, pos.y-player.position.y). As the text below the code snippet explains:

This code uses another built-in function, hypot(), and some math to calculate the speed value based on where the player node is currently located and where it’s headed. The hypot() function uses a bit of trigonometry to calculate the distance between the two points.

The two points in question are the player sprite’s position and the position you tapped on. hypot is taking the 2d distance between the two points, but the player sprite is constrained to only move horizontally – on a single dimension. We could easily compute the distance along only the x axis.

The use of hypot adds some complexity and introduces a subtle bug to the movement. If you tap one inch to the right of the player sprite on the same Y level as it (so on the platform just right of it) the sprite moves over at the speed we expect. But if you tap one inch right of the player sprite, but at the top of the screen, then the sprite moves over much slower than the previous time. That’s because the diagonal from the player at the bottom of the screen up to the top of the screen is longer than the straight line distance when we tapped just to the right. This longer distance computes that we need more time to move that far. But since we are constrained to moving only along the X axis we end up moving the same distance either way. Just faster one way versus the other.

At this point in the book it might be simpler, easier to understand, and more accurate to just compute the distance with:

let distance = abs(pos.x - player.position.x)

Marked As Solved

Paradox927

Paradox927

Author and Editor at PragProg

Hi, David.

Thank you for the suggestion.

I considered using that simpler solution as I was writing this chapter, but I ultimately decided to use the one you see in the book. I did so for the following reasons:

  1. The variation in speed when tapping on the platform versus tapping above it is marginal. Actually, most players probably wouldn’t notice the difference. I’m impressed you caught it. :grin:

  2. Later in the book, the player controls are modified to use an attached controller-knob (for lack of a better term), which also removes the ability to tap the screen to move, consequently removing the minor speed consistency issue.

  3. Readers will likely want to apply this technique to their own games, which may include a character that has a full range of motion. That being the case, I wanted to use this solution, here, because new developers may not know about the hypot() function whereas the abs() function is more common. To be honest, this reason was the primary motivating factor.

After reading your comment, I considered adding a note somewhere about using abs() as an alternative, but this being an already complex topic, I don’t want to muddy the waters. I may, eventually, add a note, but for now, I’m going to leave it as-is.

Thanks again for all of your recent comments. I appreciate your help in making this book a valuable and acurate resource.

Popular Prag Prog topics Top

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

Latest in PragProg

View all threads ❯