how to get sprite mesh point data at runtime?

0 favourites
  • 11 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • Looking for a bit of help with meshes.

    Due to Constructs lack of being able to retrieve mesh point positions at runtime through events, here's what am wanting to do...

    Edit the sprite mesh shape in the C3 editor.

    At runtime save the sprite data to json.

    Parse the json data, and store them some where accessible, to get the xy position of each mesh point so i can then set other sprite objects to their positions, or further edit the shape at runtime.

    Thanks.

    Tagged:

  • Add the json object to your project and in events json: parse sprite.asJSON.

    Then you can access mesh points with

    JSON.Get("w.mesh.points.0.0.x")

    The 0.0. part is the row then the column.

    Heres how I found that. I added a sprite and set the mesh size to 2,2 and then set point 0,0 to have an xy of 0.5, 0.5. Then looking at the mesh part of the sprite.json we have:

    "mesh":{"cols":2,"rows":2,"points":[[{"x":0.5,"y":0.5,"z":0,"u":0,"v":0},{"x":1,"y":0,"z":0,"u":1,"v":0}],[{"x":0,"y":1,"z":0,"u":0,"v":1},{"x":1,"y":1,"z":0,"u":1,"v":1}]]}

    So all the mesh data is fairly easily accessible.

  • Realized you probably what the layout position from mesh point coordinates too. I made an old post about it but search isn’t cooperating.

    You’d start with the position from the json, then using the original sprite’s position and size you convert to a layout position. The third action is if the original sprite had any rotation.

    Sprite2: set position to JSON.Get("w.mesh.points.0.0.x"), JSON.Get("w.mesh.points.0.0.y")

    Sprite2: set position to (self.x-0.5)*sprite.width+sprite.x, (self.y-0.5)*sprite.height+sprite.y

    Sprite2: set position to (self.x-sprite.x)*cos(sprite.angle)-(self.y-sprite.y)*sin(sprite.angle)+sprite.x, (self.x-sprite.x)*sin(sprite.angle)+(self.y-sprite.y)*cos(sprite.angle)+sprite.y

    Anyways hope that helps.

    Edit:

    Forgot to talk about the 0.5 part in the formulas. That would be the origin position of the sprite. 0.5,0.5 for center, and 0,0 for top left.

    Edit2:

    For zelevation too it would be:

    Sprite2: set zelevation to JSON.Get("w.mesh.points.0.0.z")+sprite.zelevation

  • Thanks for taking the time to help and explain ROJOhound.

    I was using unlerp(sprite.bboxleft,sprite.bboxright,meshpoints x pos)

    etc to get the layout xy position but it only seemed to work within the confines of the meshes bounding box.

  • Yeah referencing the bounding box doesn’t really work for that. Mesh coords are relative to the oriented box of the sprite if it had no distort.

    Basically the unlerp would only work if the sprite wasn’t rotated and if the bounding box was the same as if there was no distort.

  • Ah ok. Thanks again. 👍

    Ashley can we please get these as object expressions like poly points and image points, for better ease of use?

    Also i think given polygons are associated with meshes, they should be renamed to collision points in the editor?

    So we would have collision points, image points and mesh points.

  • Happy new years everyone! 2025!!! how crazy is that uh?

    Anywhoooo....

    see if this cap3 helps u in any way... dont remember how i did that but is at runtime...

    construct.net/en/free-online-games/radar-chart-mesh-23203/play

    should be a download button on the page.

    hope it helps!!!

    as i remember all the mesh points are editable at runtime, and u can call them by their id i think there is some trick on the mesh ids as they arent like 0,1,2,3,4,5 but XY ordonance as a XY collumns and rows in an array ..... hope makes sense...

    as for colisions, i think the moment u modify the mesh points u also modify the collision points... is just the boundry of the image is set for screen as a rectangle there was some issue there ... but this file above is basically a re-iteration from the first tutorial of terrain adjustable collision mask from c3...

    Also i dont remember if the mesh ids work for sprites, it might be only available for tiledbackgrounds... notice the green image i used...

  • Thanks for your help.

    I got ROJO's method working without much trouble thanks to his explanations.

    I'm already familiar with Meshes. Setting a mesh point to a layout position wasn't the problem, it was the other way around, getting the points position from an already set mesh since there's no inbuilt expressions, its not a problem if the point was set by another object since you already have something to go on.

  • Just noticed that in the json columns and rows appear to be reversed.

    for example, if you edit a mesh in the editor the point will say 1,4 etc, but in the json this point will actually correspond to 4,1.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yeah it’s row,column instead of column,row

  • Just noticed that in the json columns and rows appear to be reversed.

    for example, if you edit a mesh in the editor the point will say 1,4 etc, but in the json this point will actually correspond to 4,1.

    this good to know. i remember something looked weird when i was fiddling with it also, cause the numbering was a bit "speshal" xD .

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)