ByteArena / box2d

Box2D.go - Go port of Box2D - a 2D Physics Engine for Games.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Runtime error: index out of range

Pixdigit opened this issue · comments

I just got into Box2d but as I wrote a simple game engine it panicked.
Relevant files are here
https://pastebin.com/R0Yc6sCg
https://pastebin.com/QzKJeaDu

It is a very late evening for me so I might just have done a silly mistake

Could you please provide the stack trace?

I tried by moving all the calls to box2d into your entrypoint (because I don't want to install sdl).

This is my test:

package main

import "github.com/ByteArena/box2d"

var World box2d.B2World

func main() {
	gravity := box2d.MakeB2Vec2(0.0, -10.0)
	World = box2d.MakeB2World(gravity)

	bodyDef := box2d.MakeB2BodyDef()
	bodyDef.Position.Set(0, -10)
}

I have no errors. Could you run it on your side?

sdl might call box2d functions directly. We should see them in the stack trace.

PS: what you're working on looks really interesting, is it open-source by any chance?

Sorry forgot. Here it is:
https://pastebin.com/kjYSVc1m

And yes to both other questions:
It runs without errors and it will be open source. Its just, there are only a couple commits just yet and around 50 lines so I haven't bothered yet to put it up on github. (Also I'm a beginner at go so don't expect well thought through code.

Hello @Pixdigit,

Just slapped your code in a file and it seems to work (at least the parts you communicated).
Could you maybe increment on this piece of code to provide a runnable func main() that reproduces the bug you encounter ?

package main

import (
	"fmt"

	"github.com/bytearena/box2d"
)

func main() {

	gravity := box2d.MakeB2Vec2(0.0, -10.0)
	World := box2d.MakeB2World(gravity)

	bodyDef := box2d.MakeB2BodyDef()
	bodyDef.Position.Set(0, -10)
	body := World.CreateBody(&bodyDef)
	shape := box2d.MakeB2CircleShape()
	shape.B2Shape.M_radius = 0.5
	body.CreateFixture(shape, 0.0)

	timeStep := 1.0 / 60.0
	velocityIterations := 8
	positionIterations := 3

	// This is our little game loop.
	for i := 0; i < 60; i++ {
		World.Step(timeStep, velocityIterations, positionIterations)

		// Now print the position and angle of the body.
		position := body.GetPosition()
		angle := body.GetAngle()
		msg := fmt.Sprintf("%4.3f %4.3f %4.3f\n", position.X, position.Y, angle)
		fmt.Print(msg)
	}
}

Yes @netgusto your example seems to include all the functions from the stack trace. That's a good example.

@netgusto The function you provided works as intended (As to working properly and not creating an error). So for sake of ease I will just upload it to github. https://github.com/Pixdigit/GameJam/tree/8d781337d8f4875e6c87c52672b73f80e9ea4f83/src

Oh I found the error. The world is initialized in the PhysicsEngine.go in the function Test. This however was not called yet. So world is the zero value of box2d.B2World . Which then caused the error. Just as I suspected. A silly mistake.

Glad you found it :)