Code center > Darwinbots3
Very simple sound idea
Botsareus:
So this approach like any has advantages and disadvantages:
advantages: Fast
disadvantages: Exponentially losing accuracy with distance
I envision two new sysvars, one to store the 'flavor' of the sound and another to store the 'traveling distance' of a sound. Charge exponentially more energy for more powerful sound.
You will find a lot of ugliness in my code, but I just wanted to get the idea out as fast as possible.
--- Code: ---Option Explicit
Private Type Point
X As Single
Y As Single
End Type
Private Type Rect
p1 As Point
p2 As Point
End Type
Private Type Velocity
ang As Double
speed As Double
End Type
Private Type SoundPoint
location As Point
vel As Velocity
End Type
Private Type SoundCopy
location As Point
vel As Velocity
active As Boolean
End Type
Private Type RectSections
Hit As Boolean
TopLeft As Boolean
TopRight As Boolean
End Type
Private Square As Rect
Private ClientBorders As Point
Private soundpoints(15) As SoundPoint
Private soundcopys(15) As SoundCopy
Private mousepoz As Point
Private Const pi As Double = 3.14159265358979
Private wavestrength As Byte
Private waveflawor As Byte
Private Sub Form_Load()
Setup
Simulate
End Sub
Private Sub Simulate()
Do
CalculatePoints
FormatScreen
DrawRect
DrawWave
DoEvents
Loop
End Sub
Private Function CalcRectSections(pt As Point) As RectSections
CalcRectSections.Hit = pt.X > Square.p1.X And pt.X < Square.p2.X And pt.Y > Square.p1.Y And pt.Y < Square.p2.Y
'The following is real ugly because we assume it is a square in middle screen
CalcRectSections.TopRight = pt.Y < pt.X
CalcRectSections.TopLeft = pt.Y < (2000 - pt.X)
End Function
Private Sub DrawWave()
Dim a As Byte
Dim b As Byte
For a = 0 To 15
b = (a + 1) Mod 16
Line (soundpoints(a).location.X, soundpoints(a).location.Y)-(soundpoints(b).location.X, soundpoints(b).location.Y), wavecolor
'The following is real ugly, because we assume we only going to have one copy
If soundcopys(b).active And (Not soundcopys(a).active) Then
Line (soundpoints(a).location.X, soundpoints(a).location.Y)-(soundcopys(b).location.X, soundcopys(b).location.Y), wavecolor
End If
If soundcopys(b).active And soundcopys(a).active Then
Line (soundcopys(a).location.X, soundcopys(a).location.Y)-(soundcopys(b).location.X, soundcopys(b).location.Y), wavecolor
End If
If (Not soundcopys(b).active) And soundcopys(a).active Then
Line (soundcopys(a).location.X, soundcopys(a).location.Y)-(soundpoints(b).location.X, soundpoints(b).location.Y), wavecolor
End If
Next
End Sub
Private Function wavecolor() As Long
Select Case waveflawor
Case 0: wavecolor = RGB(255 - wavestrength, 255 - wavestrength, 0)
Case 1: wavecolor = RGB(0, 255 - wavestrength, 255 - wavestrength)
Case 2: wavecolor = RGB(255 - wavestrength, 0, 255 - wavestrength)
End Select
End Function
Private Sub CalculatePoints()
WaveReset
WaveUpdate
WaveSquareInteractions
End Sub
Private Sub WaveSquareInteractions()
Dim a As Byte
Dim result As RectSections
For a = 0 To 15
With soundpoints(a)
result = CalcRectSections(.location)
If result.Hit And .vel.speed = 3 Then 'I am using the speed itself to figure out where the wave originated (ugly)
If result.TopLeft And result.TopRight Then
makecopy a
.vel.ang = pi * 2 - .vel.ang
End If
If (Not result.TopLeft) And (Not result.TopRight) Then
makecopy a
.vel.ang = pi * 2 - .vel.ang
End If
If (Not result.TopLeft) And result.TopRight Then
makecopy a
.vel.ang = pi - .vel.ang
End If
If result.TopLeft And (Not result.TopRight) Then
makecopy a
.vel.ang = pi - .vel.ang
End If
End If
If (Not result.Hit) And .vel.speed = 1.5 Then
.vel.speed = .vel.speed * 2
End If
End With
Next
End Sub
Private Sub makecopy(a As Byte)
'The following is real ugly, because we assume we dealing with only one shape
soundcopys(a).active = True
soundcopys(a).vel = soundpoints(a).vel
soundcopys(a).location = soundpoints(a).location
soundcopys(a).vel.speed = soundcopys(a).vel.speed / 2
End Sub
Private Sub WaveUpdate()
Dim a As Byte
For a = 0 To 15
With soundpoints(a)
.location.X = .location.X + Cos(.vel.ang) * .vel.speed
.location.Y = .location.Y + Sin(.vel.ang) * .vel.speed
End With
With soundcopys(a)
If .active Then
.location.X = .location.X + Cos(.vel.ang) * .vel.speed
.location.Y = .location.Y + Sin(.vel.ang) * .vel.speed
End If
End With
Next
End Sub
Private Sub WaveReset()
Dim a As Byte
'Increment wave position
wavestrength = wavestrength + 1
If wavestrength = 255 Then
'if wave faded out start a new one
wavestrength = 0
waveflawor = Int(Rnd * 3)
For a = 0 To 15
soundcopys(a).active = False
soundpoints(a).location = mousepoz
If CalcRectSections(mousepoz).Hit Then
soundpoints(a).vel.speed = 1.5
Else
soundpoints(a).vel.speed = 3
End If
soundpoints(a).vel.ang = a / 15 * pi * 2
Next
End If
End Sub
Private Sub FormatScreen()
Cls
Dim ClientSize As Point
ClientSize.X = Width - ClientBorders.X
ClientSize.Y = Height - ClientBorders.Y
If ClientSize.X > ClientSize.Y Then
ScaleHeight = 2000
ScaleWidth = 2000 * ClientSize.X / ClientSize.Y
ScaleLeft = (ScaleHeight - ScaleWidth) / 2
ScaleTop = 0
ElseIf ClientSize.X < ClientSize.Y Then
ScaleWidth = 2000
ScaleHeight = 2000 * ClientSize.Y / ClientSize.X
ScaleTop = (ScaleWidth - ScaleHeight) / 2
ScaleLeft = 0
Else
ScaleWidth = 2000
ScaleHeight = 2000
End If
End Sub
Private Sub DrawRect()
Line (Square.p1.X, Square.p1.Y)-(Square.p2.X, Square.p1.Y), vbWhite
Line (Square.p1.X, Square.p1.Y)-(Square.p1.X, Square.p2.Y), vbWhite
Line (Square.p2.X, Square.p2.Y)-(Square.p2.X, Square.p1.Y), vbWhite
Line (Square.p2.X, Square.p2.Y)-(Square.p1.X, Square.p2.Y), vbWhite
End Sub
Private Sub Setup()
Show
ClientBorders.X = Width - ScaleWidth
ClientBorders.Y = Height - ScaleHeight
BackColor = vbBlack
AutoRedraw = True
Square.p1.X = 800
Square.p1.Y = 800
Square.p2.X = 1200
Square.p2.Y = 1200
Width = 8000
Height = 8000
Caption = "Sound Demo"
Icon = Nothing
MousePointer = vbCrosshair
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
mousepoz.X = X
mousepoz.Y = Y
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End
End Sub
--- End code ---
Peter:
I wonder if it would be fast in reality.
Botsareus:
I bet you Numsgil has faster and cleaner ways to implement my proposal above.
Botsareus:
Numsgil, do you like my idea or not?
Numsgil:
Okay, well, let's break it down.
First, why do we want sound? That is, what are the use cases we expect for it?
There are two primary uses I can think of:
* Locating prey
* Calling for mates, giving warning calls, etc.
Locating prey is a passive mechanism. It involves mostly listening for the sounds that a prey creature makes, and using it to locate them. Not really the production of sound on purpose (ecolocation notwithstanding). That's not unique to sound, of course. Vision and smell have the same role. Vision provides the most accurate long range information, but requires a relatively sparse environment, so it's less useful, say, underground. Smell provides fairly good information and doesn't require line of sight, but it's very susceptible to a time delay, currents/winds, etc. Sound provides very little information in and of itself, but gives you a fairly precise location for the source, is essentially instantaneous, and can be overloaded with additional information on purpose (eg bird songs).
Speaking of which, that's the second item. This is an active mechanism. For anyone in the US, there's a good David Attenborough documentary on Netflix that talks about bird calls. There's a couple key mechanisms at play here. First, high pitched noises are harder to locate precisely. This is because they tend to reflect off of different sources, masking their origin. But they're also quite short ranged because of that. They're useful for "anonymous" messages. Danger calls, etc. Lower pitched noises can be located much easier, and travel much further. They're useful for making yourself conspicuous. Either intimidating others, or distraction, or helping others locate you because you're lost. So pitches become important.
Also, there's a time component to noise. That is, bird song is not just a single straight pitch. It goes up and down and generally caries on a "tune". Information is encoded in this time signal. Our brains (and bird brains) do a good job of decoding this tumult of noises in to its constituent pitches as functions of time. The amount of information that can be conveyed is limited, however, by the acoustical properties of the environment. If the environment tends to muffle sounds, calls have to be short and loud. If the environment can carry sounds better, the calls can grow in complexity and tempo.
Next, let's look at the physics of sound. First, what we think of as "sound" is actually a time varying 1 dimension signal. That's it. The full sound signal is all garbled together in to that single dimension. Our brains are just able to decompose it in to pitches, using something probably like a discrete fourier transform. That signal propagates through air (or water) as a longitudinal wave. Importantly, it can echo. More importantly, it can diffuse, much like light can, and create "ambient" noise. This is high pitch noises are hard to locate. It's the same way that it's not always obvious where a light is coming from in a well lit room.
Also, to continue the light analogy, there's such a thing as an acoustic shadow.
Continuous sound is essentially a production of a continuous disturbance, which caries energy over time (basically, see this article). Importantly, note that pitch and energy are not related. Low pitch sounds carry energy more efficiently, because it won't get reflected or absorbed by things as readily. But there's nothing that makes high pitch easier or hard to produce than low notes.
Very low notes we don't even perceive as sound anymore. It feels like a vibration. In a fluid model, it would simply be a disturbance, which you could detect as changes in the surface velocity of the fluid at a boundary over time. Of course, if we're thinking about something like Darwinbots where there's a distinctive, discrete "step", the Nyquist frequency means we can't really detect any sounds naturally in the fluid that have a frequency higher than .5 Hz. Ah well. But for passive predator-prey hunting, detecting disturbances is all that's required, and that information is well carried by the changes in fluid, which is a natural consequence of a simulated fluid system.
For sending signals, though, the fluid system isn't sufficient. Simulating a full sound system is tricky, because of the reflection/echo. It basically puts it on par with a ray tracer in that regard. Yuck.
So I don't have a complete proposal or anything, but that's the sort of stuff I'd think about for a sound system. More of a brain dump than anything at this point. But it's Friday and I'm tired :D
Navigation
[0] Message Index
[#] Next page
Go to full version