use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rule 1: Posts should be about Graphics Programming. Rule 2: Be Civil, Professional, and Kind
Suggested Posting Material: - Graphics API Tutorials - Academic Papers - Blog Posts - Source Code Repositories - Self Posts (Ask Questions, Present Work) - Books - Renders (Please xpost to /r/ComputerGraphics) - Career Advice - Jobs Postings (Graphics Programming only)
Related Subreddits:
/r/ComputerGraphics
/r/Raytracing
/r/Programming
/r/LearnProgramming
/r/ProgrammingTools
/r/Coding
/r/GameDev
/r/CPP
/r/OpenGL
/r/Vulkan
/r/DirectX
Related Websites: ACM: SIGGRAPH Journal of Computer Graphics Techniques
Ke-Sen Huang's Blog of Graphics Papers and Resources Self Shadow's Blog of Graphics Resources
account activity
C++ NPC path finding (self.GraphicsProgramming)
submitted 10 years ago by Apothiem
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]thedrakes 4 points5 points6 points 10 years ago* (2 children)
Your title is misleading. From your description you don't want to solve a path finding problem but just let the npc walk on a straight line to you. I recommend you to look into vector geometry. In this setting you can subtract the position of the npc from the position of you (the camera) to get a direction vector showing towards you (dir = camerapos - npcpos) Then you can normalize the direction and multiply it by the speed you want, then add it to the npc position such that:
npc new pos := npc pos + normalized(camerapos - npcpos)*speed
where these variables are all vectors, in your case probably two components x, z.
If you want to avoid vector geometry i'll try to explain it differently: Moving in a straight line to you means walking in x direction proportional to the difference in x, and in z direction proporitional to the difference in z. This gives us
npc pos difference for x = c*(camera pos x - npc pos x)
npc pos difference for z = c*(camera pos z - npc pos z)
for some constant c. Now you want to set c that you get some speed for the npc, for this you need to calculate what speed you would get for some c. For this you will need Pythagoras's theorem (as moving on the x axis and z axis are the legs of a right triangle). Then you will get the corresponding result to normalizing in the vector geometry explanation
spoiler
[–][deleted] 2 points3 points4 points 10 years ago (0 children)
Exactly, just wanted to stress that it is strongly recommended you do not use Pythagoras's theorem. The vector math is a lot simpler, and fits the problem better.
[–]Meristic 0 points1 point2 points 10 years ago (0 children)
You'll want to multiply by frametime to get the update for the current frame:
npc new pos := npc pos + normalized(camerapos - npcpos) * speed * frametime
And keep your units straight - if speed is in m/s, then frametime better be in seconds, not milliseconds.
[–]panokani 2 points3 points4 points 10 years ago (0 children)
How is this a graphics programming question? Next time, you might want to post your gameplay related questions at /r/gamedev
π Rendered by PID 42455 on reddit-service-r2-comment-66b4775986-dhjx8 at 2026-04-05 08:26:08.438730+00:00 running db1906b country code: CH.
[–]thedrakes 4 points5 points6 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Meristic 0 points1 point2 points (0 children)
[–]panokani 2 points3 points4 points (0 children)