Code Monkey home page Code Monkey logo

box2d's Introduction

Byte Arena

arena-trainer release

GITHUB_API_TOKEN=TOKEN ./scripts/make-release-trainer.sh RELEASE

mq-cli release

./scripts/make-mq-cli.sh

box2d's People

Contributors

alexander-r avatar dolanor avatar maxfish avatar tanayseven avatar wingyplus avatar xtuc avatar yome avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

box2d's Issues

Collisions problem

Hello, guys
I'm trying to use your engine in my own game and found that the are no collisions between bodies occurs at all.
I created a small test with your code from cpp_compliance_test.go, and I see that there the same situation.

This is a small test - a small dinamic circle body above ground Edge. Standard gravity (0,-10), and you can see that this circle body fall down and pass through the edge to negative Y axis.

Is there something wrong in this code that I copied from your cpp_compliance_test.go, or collisions between dynamic/static are not supported yet?

package main

import (
	"fmt"
	"testing"

	"github.com/ByteArena/box2d"
)

func TestBox2dCollisions(t *testing.T) {

	world := box2d.MakeB2World(box2d.MakeB2Vec2(0, -10))

	// ground body (from cpp_compliance_test.cpp)
	{
		bd := box2d.MakeB2BodyDef()
		ground := world.CreateBody(&bd)
		shape := box2d.MakeB2EdgeShape()
		shape.Set(box2d.MakeB2Vec2(-20.0, 0.0), box2d.MakeB2Vec2(20.0, 0.0))
		ground.CreateFixture(&shape, 0.0)
	}

	// circle character (from cpp_compliance_test.cpp)
	bd := box2d.MakeB2BodyDef()
	bd.Position.Set(0, 10.0)
	bd.Type = box2d.B2BodyType.B2_dynamicBody
	bd.FixedRotation = true
	bd.AllowSleep = false

	body := world.CreateBody(&bd)

	shape := box2d.MakeB2CircleShape()
	shape.M_radius = 0.5

	fd := box2d.MakeB2FixtureDef()
	fd.Shape = &shape
	fd.Density = 200.0
	body.CreateFixtureFromDef(&fd)

	// Prepare for simulation. Typically we use a time step of 1/60 of a
	// second (60Hz) and 10 iterations. This provides a high quality simulation
	// in most game scenarios.
	timeStep := 1.0 / 60.0
	velocityIterations := 8
	positionIterations := 3

	for i := 0; i < 100; i++ {
		// Instruct the world to perform a single step of simulation.
		// It is generally best to keep the time step and iterations fixed.
		//runtime.Breakpoint()
		world.Step(timeStep, velocityIterations, positionIterations)
		fmt.Printf("circle character coordinates: %v,%v\n", body.GetPosition().X, body.GetPosition().Y)
	}
}

Output is:

=== RUN   TestBox2dCollisions
circle character coordinates: 0,9.997222222222222
circle character coordinates: 0,9.991666666666665
circle character coordinates: 0,9.983333333333333
circle character coordinates: 0,9.972222222222221
circle character coordinates: 0,9.958333333333332
........etc
........
circle character coordinates: 0,1.2222222222222219
circle character coordinates: 0,0.9999999999999999
circle character coordinates: 0,0.7750000000000001
circle character coordinates: 0,0.5472222222222226
circle character coordinates: 0,0.3166666666666673
circle character coordinates: 0,0.08333333333333426
circle character coordinates: 0,-0.15277777777777657
circle character coordinates: 0,-0.39166666666666516
circle character coordinates: 0,-0.6333333333333315
circle character coordinates: 0,-0.8777777777777757
circle character coordinates: 0,-1.1249999999999976
circle character coordinates: 0,-1.3749999999999973
circle character coordinates: 0,-1.6277777777777747
circle character coordinates: 0,-1.8833333333333298
circle character coordinates: 0,-2.1416666666666626
circle character coordinates: 0,-2.4027777777777732
circle character coordinates: 0,-2.6666666666666616```

How to create joint?

box2d doc says joint must be created by create method in B2world.

func (world *B2World) CreateJoint(def *B2JointDef) B2JointInterface {

But CreateJoint method receives B2JointDef, not B2JointDefInterface.
I cannot pass *B2PrismaticJointDef as *B2JointDef and even passing &B2PrismaticJointDef.B2JointDef will not work because def.(type) is B2JointDef, not B2PrismaticJointDef.
So type asserting will fail.

How can I create various joints?

Simulation instability caused by some joints

It looks like some of the joints types don't work properly.

I'm implementing a loader for R.U.B.E scenes here https://github.com/maxfish/gojira2d/blob/master/pkg/physics/b2djson_scene.go and I'm trying a lot of different scenes.
I can see that the only joints which work well are revolute, rope, distance and wheel. Motor, prismatic, weld and friction seem to make the simulation explode after a while.

Here is one example of a weld join that is unstable: https://pastebin.com/dPB2xBfV
As soon as body3, while falling, touches body2, body2 sprints away from the screen at high speed

Another example simulating a R.U.B.E scene containing all types of joints:

Any ideas of what could be the cause?

Creating a revolute joint?

Following the C++ API:

b2RevoluteJointDef jointDef;
b2RevoluteJoint* joint = (b2RevoluteJoint*)myWorld->CreateJoint(&jointDef);

I have tried:

jointDef := box2d.MakeB2RevoluteJointDef()
joint := (*box2d.B2RevoluteJoint)(world.CreateJoint(&jointDef))

which results in the error: cannot convert world.CreateJoint(&jointDef) (value of type box2d.B2JointInterface) to *box2d.B2RevoluteJoint

can you give an example on how to create a joint?

DynamicsB2JointFriction Dump method has a Pointer Reciever

I'm a noob to box2d (and Go), I was following a C++ example on creating joints.
In the example:

b2xxxJoint* joint = (b2xxxJoint*)world->CreateJoint( &jointDef );

They cast the returned joint to the one they wanted created. Doing the same in Go gives a compilation error

impossible type assertion:
box2d.B2FrictionJoint does not implement box2d.B2JointInterface (Dump method has pointer receiver)

var jointDef box2d.B2FrictionJointDef = box2d.MakeB2FrictionJointDef()
jointDef.BodyA = ball1
jointDef.BodyB = ball2
joint := world.CreateJoint(&jointDef).(box2d.B2FrictionJoint)

Looking at the source and you can see it's a pointer receiver B2JointFrictio Line 282

Some of the other joints are not B2JointDistance Line 337

Large circle vs small circle

I have two dynamic circle bodies. One is small and the other is large. When the small circle collides with the large circle, box2d contact occurs early then the figures touch each other. I'll attach a visualization of this situation.
image

Debug draw?

I see some code about debug draw, but they are commented out. Is there any preferred way to draw debug shapes?

Nothing Moves

I followed the instructions for the hello world program, in the PDF linked from the README. And nothing moves. Perhaps I did something wrong.

func main() {

	var gravity = d2.MakeB2Vec2(0.0, -10.0)
	var world = d2.MakeB2World(gravity)

	groundBodyDef := d2.B2BodyDef{}
	groundBodyDef.Position.Set(0, -10)
	groundBody := world.CreateBody(&groundBodyDef)
	groundBox := d2.B2PolygonShape{}
	groundBox.SetAsBox(50, 10)
	groundBody.CreateFixture(&groundBox, 0)

	bodyDef := d2.B2BodyDef{}
	bodyDef.Type = d2.B2BodyType.B2_dynamicBody
	bodyDef.Position.Set(0, 4)
	body := world.CreateBody(&bodyDef)
	dynamicBox := d2.B2PolygonShape{}
	dynamicBox.SetAsBox(1, 1)
	fixtureDef := d2.B2FixtureDef{}
	fixtureDef.Shape = &dynamicBox
	fixtureDef.Density = 1
	fixtureDef.Friction = 0.3
	body.CreateFixtureFromDef(&fixtureDef)

	timeStep := 1.0 / 60.0
	velocityIterations := 6
	positionIterations := 2

	for i := 0; i < 60; i++ {
		world.Step(timeStep, velocityIterations, positionIterations)
		position := body.GetPosition()
		angle := body.GetAngle()
		fmt.Printf("%4.2f %4.2f %4.2f\n", position.X, position.Y, angle)
	}
}
$ go run main.go 
0.00 4.00 0.00
0.00 4.00 0.00
0.00 4.00 0.00
0.00 4.00 0.00
0.00 4.00 0.00
0.00 4.00 0.00
...

Truthfully I tried incorporating this module into another project I'm working on and it failed there. Which is when I took a step back, tried the hello world, and found that didn't work either.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.