Search Results
!!b2oSUmilA2N/g/105992556#105997028
7/23/2025, 11:12:30 AM
>>105995717
>there is no way to access cloth.points.pos as a continuous array right?
why?
#soa directive tells the compiler to allocate the data with a slightly different layout but apart from that, its just your regular array. Its perfect for doing ECS stuff.
Also, there are a few things I'd like to point out.
if you are looking for performance, ditch dynamic arrays. They will do implicit allocations and depending on the usage, it can be slow. Calling make() and delete() on []Point and []Stick arrays is very straightforward.
A reason why cloth.points.pos might not be working is that in the cloth sim example, they used an array of pointers for each of points and sticks while calling new() for every individual element allocation and appending that with dynamic's implicit allocator. Depending on how the OS ends up allocating these points, there will a hit on performance due to cache misses.
You are using the direct struct type themselves as arrays so you'll have to do cloth.points[0].pos to access the pos element at that particular index, after prior memory allocation with make(), of course.
If I understood what you are trying to do correctly, here is a sample code
Cloth :: struct {
points : #soa []Point,
stick : #soa []Stick,
}
cloth : Cloth
cloth.points = make(#soa[]Point, width * height) // allocate cloth's WIDTH * HEIGHT number of Point types in soa
cloth.sticks = make(#soa[]Stick, width * height)
// you can access and assign the values to points like you would with normal arrays
cloth.points[0].pos = {10, 10}
// you can also access the elements of structs as if the elements were arrays
fmt.println(cloth.points.pos[0]) // prints [10, 10]
// but don't try to assign values the latter way, there is an llvm backend issue with this syntax
cloth.points.pos[0] = {10, 10} // avoid. doesn't always work. bug.
>there is no way to access cloth.points.pos as a continuous array right?
why?
#soa directive tells the compiler to allocate the data with a slightly different layout but apart from that, its just your regular array. Its perfect for doing ECS stuff.
Also, there are a few things I'd like to point out.
if you are looking for performance, ditch dynamic arrays. They will do implicit allocations and depending on the usage, it can be slow. Calling make() and delete() on []Point and []Stick arrays is very straightforward.
A reason why cloth.points.pos might not be working is that in the cloth sim example, they used an array of pointers for each of points and sticks while calling new() for every individual element allocation and appending that with dynamic's implicit allocator. Depending on how the OS ends up allocating these points, there will a hit on performance due to cache misses.
You are using the direct struct type themselves as arrays so you'll have to do cloth.points[0].pos to access the pos element at that particular index, after prior memory allocation with make(), of course.
If I understood what you are trying to do correctly, here is a sample code
Cloth :: struct {
points : #soa []Point,
stick : #soa []Stick,
}
cloth : Cloth
cloth.points = make(#soa[]Point, width * height) // allocate cloth's WIDTH * HEIGHT number of Point types in soa
cloth.sticks = make(#soa[]Stick, width * height)
// you can access and assign the values to points like you would with normal arrays
cloth.points[0].pos = {10, 10}
// you can also access the elements of structs as if the elements were arrays
fmt.println(cloth.points.pos[0]) // prints [10, 10]
// but don't try to assign values the latter way, there is an llvm backend issue with this syntax
cloth.points.pos[0] = {10, 10} // avoid. doesn't always work. bug.
Page 1