python - Equations for walking forward -


i wrote simple 3d engine pygame. i'm trying create simple first person controller moving around scene. engine assumes 3d grid player starting facing negative z axis. camera can rotate 3 axis if @ origin. first person controller can rotate around y axis - i.e. around 360 degrees. i'm trying make wasd controls move relative direction camera looking at.

here have (self.camera camera object 3d engine, has attributes x, y, z (position) , x_rot, y_rot, z_rot (rotation):

import python3d p3d import pygame import math  class first_person_controller:     def __init__(self,scene,camera,end_func):         pygame.event.set_grab(true)         pygame.mouse.set_visible(false)         self.scene = scene         self.camera = camera         self.end_func = end_func      def fps_control(self):         keys = pygame.key.get_pressed()         relative = pygame.mouse.get_rel()         self.camera.change_camera_rot([0,relative[0]/1000*-1,0])         if keys[pygame.k_w]:             self.camera.z += math.cos(self.camera.y_rot) * 5 * -1             self.camera.x += math.sin(self.camera.y_rot) * 5 * -1         if keys[pygame.k_s]:             self.camera.z += math.cos(self.camera.y_rot) * 5             self.camera.x += math.sin(self.camera.y_rot) * 5         if keys[pygame.k_a]:             self.camera.z += math.cos(self.camera.y_rot + math.radians(90)) * 5 * -1             self.camera.x += math.sin(self.camera.y_rot + math.radians(90)) * 5 * -1         if keys[pygame.k_d]:             self.camera.z += math.cos(self.camera.y_rot + math.radians(90)) * 5             self.camera.x += math.sin(self.camera.y_rot + math.radians(90)) * 5         if keys[pygame.k_escape]:             self.end_func()         if keys[pygame.k_c]:             self.camera.y = 0         elif not keys[pygame.k_c]:             self.camera.y = 5 

this supposed fit normal pygame loop like:

while true:    clear screen    fps_control()    render scene 

however, when implemented first person controller goes off course, , @ least appears not walk in straight line. can't tell if because of math, or engine.

if tell me issue - trigonometry or 3d engine, , if trigonometry suggest better equations, great.

if helps, here 3d engine, of right now, code isn't readable.

thanks.

i'll voted down it, whatever.

your code reminds me lot earlier attempt 3d engine of own. however, after facing numerous problem, figured vectors me lot, , learned matrices, these helped tons more.

and here link towards course named vector math 3d computer graphics great, should go step further using 4x4 matrices. may seems boring stuff, you'll glad see how easy logic becomes once it.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -