korlibs / korge

KorGE Game Engine. Multiplatform Kotlin Game Engine
https://korge.org/
Other
2.54k stars 125 forks source link

NPE calling PolygonShape.set #445

Closed jbellis closed 2 years ago

jbellis commented 2 years ago

As near as I can tell this is supposed to be a legitimate way to use the API:

fun main(args: Array) { val shape = PolygonShape() var zero = Vec2(0f, 0f) shape.set(arrayOf(zero, zero, zero), 3) }

->

Exception in thread "main" java.lang.NullPointerException at org.jbox2d.collision.shapes.PolygonShape.set(PolygonShape.kt:147) at org.jbox2d.collision.shapes.PolygonShape.set(PolygonShape.kt:110) at TestPSKt.main(TestPS.kt:7)

soywiz commented 2 years ago

Looking at the code this line:

https://github.com/korlibs/korge-next/blob/61d5d25b2a486338f913d9c8847850587c599f76/kbox2d/src/commonMain/kotlin/org/jbox2d/collision/shapes/PolygonShape.kt#L134

Maybe instead of creating an array of nulls, it should create an array of actual points set to 0.0, 0.0. Maybe in C it was valid since they were structures, but null pointers are not going to work later looking at the code

soywiz commented 2 years ago

This seems to happen also on jbox2d: https://github.com/jbox2d/jbox2d/blob/fa4c74cbcdb607454150db131012c9469a70c55f/jbox2d-library/src/main/java/org/jbox2d/collision/shapes/PolygonShape.java#L134

Oh, it seems that jbox2d it just using the points provided in the input even if they are mutable: https://github.com/jbox2d/jbox2d/blob/fa4c74cbcdb607454150db131012c9469a70c55f/jbox2d-library/src/main/java/org/jbox2d/collision/shapes/PolygonShape.java#L147

While in KorGE we asumed the array was not null: https://github.com/korlibs/korge-next/blob/61d5d25b2a486338f913d9c8847850587c599f76/kbox2d/src/commonMain/kotlin/org/jbox2d/collision/shapes/PolygonShape.kt#L147

So I guess we should duplicate the poitns there

jbellis commented 2 years ago

Yeah, this is messy because 99% of the code is set up to assume that vertex arrays do not contain nulls, but then this one section of code creates an array of nulls and copies in only what it needs.