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.

Where Next?

Popular Pragmatic Bookshelf topics Top

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
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
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
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
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
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
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
New

Sub Categories: