Git Product home page Git Product logo

Comments (3)

derek1906 avatar derek1906 commented on May 24, 2024 2

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.

marcwlester avatar marcwlester commented on May 24, 2024

as a work around I use body.userData = { ... };

from box2d.js.

whackashoe avatar whackashoe commented on May 24, 2024

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)

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.