Git Product home page Git Product logo

cp's Introduction

cp

GoDoc Sourcegraph

Go port of Chipmunk2D physics library.

import "github.com/jakecoffman/cp/v2"

Project status

Stable -- most features are implemented and the demos are very close to Chipmunk2D demos.

Examples

Documentation

The official chipmunk docs are a really good place to start: https://chipmunk-physics.net/release/ChipmunkLatest-Docs/

Features

Same features as Chipmunk2D:

  • Designed specifically for 2D video games.
  • Circle, convex polygon, and beveled line segment collision primitives.
  • Multiple collision primitives can be attached to a single rigid body.
  • Fast broad phase collision detection by using a bounding box tree with great temporal coherence or a spatial hash.
  • Extremely fast impulse solving by utilizing Erin Catto’s contact persistence algorithm.
  • Supports sleeping objects that have come to rest to reduce the CPU load.
  • Support for collision event callbacks based on user definable object types types.
  • Flexible collision filtering system with layers, exclusion groups and callbacks.
  • Can be used to create all sorts of effects like one way platforms or buoyancy areas. (Examples included)
  • Supports nearest point, segment (raycasting), shape and bounding box queries to the collision detection system.
  • Collision impulses amounts can be retrieved for gameplay effects, sound effects, etc.
  • Large variety of joints – easily make vehicles, ragdolls, and more.
  • Joint callbacks.
  • Can be used to easily implement breakable or animated joints. (Examples included)
  • Maintains a contact graph of all colliding objects.
  • No external dependencies.
  • Many language bindings available.
  • Simple, read the documentation and see!
  • Unrestrictive MIT license

cp's People

Contributors

dvoyni avatar gabstv avatar j7b avatar jakecoffman avatar meyerzinn avatar setanarut avatar tmiv avatar usedbytes 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

cp's Issues

Misleading "implement me" panics (?)

I dug into the official Chipmunk source, and apparently all the Destroy()s "implement me"
should be noops on the Go port, since the purpose of it was to clear from memory in ObjC, but
Go's GC takes care of it.

The Destroy() instances:

cp/bbtree.go

Line 89 in 60599de

panic("implement me")

cp/circle.go

Line 35 in 60599de

panic("implement me")

cp/segment.go

Line 41 in 60599de

panic("implement me")

cp/poly.go

Line 67 in 60599de

panic("implement me")

panic("implement me")

This is not a Destroy, but seems to be unused internally:

cp/bbtree.go

Line 328 in 60599de

panic("implement me")

Or maybe the Destroy() method can be removed from the ShapeClass interface, since it's never used internally. What do you think?

I'll create a PR. Please let me know if there's a use case I'm missing.

Support 64bit collision masks

As a optional function, obviously. It would have its uses in quite a few different cases, most notably where you need more than 32 possible collisions.

unexpected ApplyImpulseAtLocalPoint behaviour

Hi, i am having trouble using the applyImpulse function because I observe unexpected behavior.

I have a circle body to which I apply an impulse (local point = zero).
Right before and after the apply, I log the velocity of the body.
Why is it that a positive X component (here 49) is causing a new velocity in the opposite direction?

7:48AM DBG sprites/moveable_sprite.go:44 pre apply impulse vx=-0.2918252140476518 vy=24.166954594470702 fx=49 fy=-3
7:48AM DBG sprites/moveable_sprite.go:46 post apply impulse vx=-37.113445811790115 vy=-8.301002787475994

When apply an impulse on a zero-velocity situation then the resulting movement is as expected.

ShapeQueries not implemeted?

Hi! Awesome library. I have a couple specific use case that would work perfect for the shape query api that is in the C implementation but I can't seem to find it in this port? https://chipmunk-physics.net/release/ChipmunkLatest-Docs/#Queries-ShapeQueries

However I could probably work around this by adding a shape to bodies in the scene that are in the scene that don't move and add a sensor shape. This isn't ideal for many of my use cases, but I could probably make it work.

Concurrent access to queries

I noticed that chipmunk seems to have its own, non-atomic(?) locking system. Are any query functions (such as BBQuery) safe for concurrent access as long as it is not updating simultaneously?

Body

CP has UserData, very needed, and EachShape appears missing.

implement every demo

  • LogoSmash,//A
  • PyramidStack,//B
  • Plink,//C
  • BouncyHexagons,//D
  • Tumble,//E
  • PyramidTopple,//F
  • Planet,//G
  • Springies,//H
  • Pump,//I
  • TheoJansen,//J
  • Query,//K
  • OneWay,//L
  • Joints,//M
  • Tank,//N
  • Chains,//O
  • Crane,//P
  • ContactGraph,//Q
  • Buoyancy,//R
  • Player,//S
  • Slice,//T
  • Convex,//U
  • Unicycle,//V
  • Sticky,//W
  • Shatter,//X

Body AddVelocity?

I was using another version of Chipmunk for go (vova616's version ) and it had a method on the Body called AddVelocity that was very useful. I added it to your Body and it works great maybe you should add it ?

func (body *Body) AddVelocity(x, y float64) {
	body.v.X += float64(x)
	body.v.Y += float64(y)
}  

space SegmentQuery and possibly other functions that use SpaceHash SegmentQuery broken.

Hello again :) I've been using your awesome port for a bit and ran into this issue.
When using space SegmentQuery and the space is setup to use a spatial hash the engine will crash due to:

github.com/jakecoffman/cp.(*SpaceHash).SegmentQuery(0x3f91111111111101?, {0x97a100?, 0xc000694690?}, {0x4077700000000000?, 0x3ff0000000000000?}, {0x3fb999999999999a?, 0x0?}, 0x9?, 0x0?, {0x0?, ...})
        C:/Users/beebl/go/pkg/mod/github.com/jakecoffman/[email protected]/spacehash.go:239 +0x5d1
github.com/jakecoffman/cp.(*Space).SegmentQuery(0xc0006ac000, {0x3f91111111111111?, 0x7dcf65?}, {0x1ddec26e158?, 0x40?}, 0x4014000000000000, {0x0?, 0x8?, 0x1ddec210a20?}, 0x9d73f0, ...)
        C:/Users/beebl/go/pkg/mod/github.com/jakecoffman/[email protected]/space.go:1046 +0x216

This is because SpaceHash::SegmentQuery is expecting the obj interface to be an instance of Shape and not SegmentQueryContext which is passed into it by shape::SegmentQuery.

I suspect this is broken with the other query functions as well, but I haven't had time to check.

The workaround is to not configure a spatial hash.

I'm not sure if I need to use a spacial hash or not, but I need to support a few thousand collision objects in my project. I'll not use the spatial hash for now.

Here is a small example of the problem and you may comment out the line that sets up the spatial hash to see segment query does work in that case:

https://gist.github.com/steampoweredtaco/ecf8aa13ba26d4cad7e54962bc1453d7

The optimization of locks in BBQuery

Can

func (space *Space) BBQuery(bb BB, filter ShapeFilter, f SpaceBBQueryFunc, data interface{}) {
	context := BBQueryContext{bb, filter, f}
	space.Lock()

	space.staticShapes.class.Query(&context, bb, space.bbQuery, data)
	space.dynamicShapes.class.Query(&context, bb, space.bbQuery, data)

	space.Unlock(true)
}

be optimized to :

func (space *Space) BBQuery(bb BB, filter ShapeFilter, f SpaceBBQueryFunc, data interface{}) {
	context := BBQueryContext{bb, filter, f}

	space.staticShapes.class.Query(&context, bb, space.bbQuery, data)
	
	space.Lock()
	space.dynamicShapes.class.Query(&context, bb, space.bbQuery, data)
	space.Unlock(true)
}

If the vector is zero, Normalize() returns NaN.

If the vector is zero, Normalize() returns NaN. For example, If the player velocity vector is zero, an error is received. To fix it replace math.SmallestNonzeroFloat64 -> 1e-15

cp/vector.go

Line 89 in e672e20

return v.Mult(1.0 / (v.Length() + math.SmallestNonzeroFloat64))

zeroVector := cp.Vector{0, 0}
// Normalize Vector
fmt.Println(zeroVector.Mult(1.0 / (zeroVector.Length() + math.SmallestNonzeroFloat64)))
fmt.Println(zeroVector.Mult(1.0 / (zeroVector.Length() + 1e-15)))
// NaN,NaN
// 0.000000,0.000000

How to query for bodies in BB?

#As far as I can tell, Space does not expose any mechanism to query for shapes/bodies in an area (cpSpaceBBQuery in the C version). Is it possible to access the BBTree for a Space (even if it is read-only) for calculating things like field-of-view for a game, or could a BBQuery function be added?

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.