Comments (3)
Since userData
accepts a void *
you will have to allocate some space for your datatype and pass the pointer to set_userData
. Here is an example of how you store a string:
// some typedef
var char = "i8";
// Malloc space for input string and returns pointer.
function mallocString(str){
str = String(str);
var strptr = Box2D._malloc(str.length + 1, char, Box2D.ALLOC_STACK);
for(var offset = 0; offset < str.length; offset++){
Box2D.setValue(strptr + offset, str.charCodeAt(offset), char);
}
Box2D.setValue(strptr + offset, 0, char); // null terminated
return strptr;
}
// Get string back from a string pointer.
function getString(strptr){
if(strptr === 0) return "";
var builder = "", c;
while(c = Box2D.getValue(strptr++, char)){
builder += String.fromCharCode(c);
}
return builder;
}
// ...
bodyDef.set_userData(mallocString("Hello world")); // store "Hello world"
var body = world.CreateBody(bodyDef);
getString(body.GetUserData()); // "Hello world"
More on accessing memory can be found here. Remember to free the string after you destroy the body with Box2D._free
:
Box2D._free(body.GetUserData());
Box2D.destroy(body);
from box2d.js.
as a work around I use body.userData = { ... };
from box2d.js.
What I do is this: I throw the data I need in a dictionary
with a integer key, I pass this key into set_userData
and then I can look up the corresponding data from the dictionary when I do get_userData
.
from box2d.js.
Related Issues (20)
- b2Vec2 won't free HOT 2
- Unable to build latest on macOS Mojave with [email protected] HOT 5
- High Gravity jitter HOT 1
- missing runtime methods for allocating arrays HOT 4
- box2d.wasm.html test faild with [email protected] build HOT 2
- is there typescript declaration file (d.ts) for this project? HOT 2
- Box2d 2.4.0
- Box2D 2.4.0 HOT 8
- Node server side implementation
- How does this work with webpack? HOT 1
- May I ask why the calculation results are different on the ios and android platforms HOT 2
- vue how to use it
- vue hao to use it
- why is not show?
- why now show debugdraw
- could i have more simple demos about the box2djs οΌand where can i see the API about box2djs
- How to build? I want to build the debug version of javascript file
- Position not getting what is set
- Uncaught (in promise) cannot construct a b2Draw, no constructor in IDL
- Cannot SetTarget of b2MouseJoint
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from box2d.js.