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

Hm, You're right, I'm very lost on this code... I did however make it python 2 and 3 compliant I believe as well as sped it up to 1/3 it's original speed.

from math import cos, sin, sqrt, atan2, pi

def dot(a, b):
    return (a[0]*b[0]) + (a[1]*b[1])

def magnitude(a):
    """returns the sqrt of a squared + b squared"""
    return sqrt(a[0]**2 + a[1]**2)

def normalize(a):
    m = magnitude(a)
    return (a[0]/m, a[1]/m)

def pan_value(player_pos, player_facing, sound_pos):
    player_facing = player_facing*(pi/180.0)
    front = normalize((cos(player_facing), sin(player_facing)))
    right = normalize((cos(player_facing-pi/2.0), sin(player_facing-pi/2.0)))
    #translate the sound's position to be relative to the player
    translated_pos = sound_pos[0]-pla yer_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 = atan2(y, x)
    is_behind = y <0
    return (cos(angle), is_behind)

print pan_value((5, 0), 0, (2, 0))


I guess with this many equasions, it is bound to be somewhat slow, but I don't think it matters when using pygame though as I don't think you can have more than 16 sounds going at once...

_______________________________________________
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 : 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