Re: Learning how to code free movement and panning in a game like Swamp.

The following code is correct as far as I can tell.  I included the test cases.  The function pan_value returns two things: a value between -1 and 1 as above and a true/false value that tells you if the object is behind the player or not.  This is basically a 2d transformation matrix, but gameobjects is overkill so I didn't bring it in.
Possibly unlike the first post, the angle 0 is east, the angle 90 is north, the angle 180 is west, and the angle 270 is south.  If you need to make it so that 90 is south and 270 is north, you can negate the parameter as the first line of the function.  Tabs apparently break hard on this forum in the sense that NVDA at least doesn't like reading indentation indication when you use them (the forum appears to be doing some sanitizing).  Consequently, I used single-space indentation.
if you can find a test case where this is broken, post it and I'll fix the code.

from __future__ import division
import math

def dot(a, b):
 return sum(a[i]*b[i] for i in xrange(len(a)))

def magnitude(a):
 return math.sqrt(sum(i**2 for i in a))

def normalize(a):
 m = magnitude(a)
 return tuple(i/m for i in a)

def pan_value(player_pos, player_facing, sound_pos):
 player_facing=player_facing*(math.pi/180.0)
 front = normalize((math.cos(player_facing), math.sin(player_facing)))
 right = normalize((math.cos(player_facing-math.pi/2.0), math.sin(player_facing-math.pi/2.0)))
 #translate the sound's position to be relative to the player.
 translated_pos= sound_pos[0]-player_pos[0], sound_pos[1]-player_pos[1]
 #y is front because this gives an angle on the range 0 to pi instead of -pi/2 to pi/2.
 x= dot(right, translated_pos)
 y = dot(front, translated_pos)
 angle =math.atan2(y, x)
 is_behind = y < 0
 return (math.cos(angle), is_behind)

print pan_value((5, 0), 0, (2, 0))
print pan_value((5, 0), 90, (0, 0))
print pan_value((5, 0), 110, (0, 0))
print pan_value((5, 0), 270, (2, 0))
print pan_value((5, 0), 270, (2, 1))

And that's it.

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : Aprone via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : Aprone via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : frastlin via Audiogames-reflector
  • ... AudioGames . net Forum — Off-topic room : camlorn via Audiogames-reflector

Reply via email to