Different player speed for unknown reason
JKKross opened this issue · 5 comments
Weird thing happened:
I finished part 2 of this (awesome!) tutorial & had a weird feeling about how fast my "character" (blue box) was moving.
So I downloaded the source code for part 2 & just ran it on my iPhone.
And for some weird reason, the "character" in the game built from github source moves almost half the speed...
I checked the code and ignoring some added whitespace, my code & github source are identical. Same constants, same functions, same everything - I followed the tutorial step by step & copy/pasted everything as instructed.
Here are the screen recordings comparing my "version" & the source:
Archive.zip
P.S.: I'm not sure this is really an issue in the github sense - sorry if it isn't 😀
P.P.S.: I'll compare all the files again first thing in the morning - there is a chance I missed something, but I'm pretty sure I didn't (I wouldn't be opening an issue otherwise)
This is really weird 🤔 I don't have any ideas at the moment, but if you can't figure it out then upload your project and I'll take a look.
Are you by any chance running in DEBUG mode? That's usually the default when running on a device, and it severely impacts the performance. It's set to RELEASE mode in the sample project, so that might explain it.
See Part 4 for details.
Oh wait, you said your version is running faster, not slower? Never mind then.
@nicklockwood Okay, my bad - I'm really sorry.
In ViewController.swift, I overlooked that I missed one line of code.
Specifically, in:
@objc func update(_ displayLink: CADisplayLink) {
let timeStep = min(maximumTimeStep, displayLink.timestamp - lastFrameTime)
let input = Input(velocity: inputVector)
let worldSteps = (timeStep / worldTimeStep).rounded(.up)
for _ in 0 ..< Int(worldSteps) {
world.update(timeStep: timeStep / worldSteps, input: input)
}
lastFrameTime = displayLink.timestamp
let size = Int(min(imageView.bounds.width, imageView.bounds.height))
var renderer = Renderer(width: size, height: size)
renderer.draw(world)
imageView.image = UIImage(bitmap: renderer.bitmap)
}
In my version, line lastFrameTime = displayLink.timestamp
was missing.
That fixed it.
I'm really sorry for ringing the alarm bell.
No worries, thanks for following up!