GenerateCollisionMaskFromTexture() Questions

This forum is currently in read-only mode.
  • I have some questions regarding the GenerateCollisionMaskFromTexture() function.

    First of all, where's the best place in the code to call this? I've tried calling it from OnCreate(), but the runtime crashes when two of the same object are present. Right now I have it called from OnFrame(), which is set to not call again after the first time, which works, but I don't think it's the best solution.

    Also, I'm having problems regarding runtime serialization after using this function to generate a collision mask. Everytime I load from a quicksave, the runtime crashes when it tries to generate a new collision mask. Is this something wrong with my plugin or Construct?

  • I think that function was custom coded for the Canvas object. What are you trying to do with it? Can you paste some code snippets too?

  • I'm using it for my Sprite Button plugin, which is how it supports custom shaped buttons. Here's what I have now with OnFrame():

    BOOL ExtObject::OnFrame()
    	// Set the collision mask for this object
    	// iTextureD is the main button face texture
    	pRuntime->GenerateCollisionMaskFromTexture(this, iTextureD);
    	// Don't call anymore
    	return 1;
    And here's what I was doing in OnCreate():
    [code:3piu01s7]void ExtObject::OnCreate()
    	// Load the edittime data that was serialized.
    	bin ar;
    	ar.attach(info.editObject->eData, info.editObject->eSize);
    	// Read the data.  Same format as you exported in EditExt::Serialize.
    	// Your runtime loader must be able to load all versions!
    	int Version = 0;
    	ar >> Version;
    	// Texture loader thingy
    	ImageHandleInfo* imgTexture;
    	// Load the default texture
    	imgTexture = pRuntime->LoadSerializedImageHandle(ar);
    	iTextureD = renderer->CreateTextureFromHandle(imgTexture);
    	// Update the object's collision mask
    	pRuntime->GenerateCollisionMaskFromTexture(this, iTextureD);
    The first method works fine until I load from a runtime save, and the second one crashes upon loading a runtime save or when there are multiples of the object.
    Also, as a side note, the serialization code(not shown) is merely loading/saving some variables, so I don't think it's causing any problems.
  • You do realise that all textures have a collision mask already created by Construct automatically? You only need that function when you're creating a render target and rendering custom shapes on to that, in which case the collision mask will have changed. But I would strongly advise against using render targets to render something as simple as a button.

  • How do I go about making use of a texture's collision mask? Does it have something to do with info.curTexture or info.imgHandle?

    Also, my drawing code is just using renderer->SetTexture(...), then renderer->Quad_xywh(...). Is there some other way I should be drawing things?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If I remember correctly, you need to set info.collMode to COLLISIONMODE_FINE to enable per pixel collisions, then info.imgHandle will be used as the collision mask. Don't forget the bounding box has to be correct for that.

  • That does indeed seem to work, however, I'm still having serialization problems. I'm not having crashing problems or anything, but upon loading of a runtime save, the collision mask seems to be forgotten. Here's the serialization code(the three dots just represent the other variables I'm serializing):

    void ExtObject::Serialize(bin& ar)
    	if (ar.loading) {
    		// Serialize all runtime info
    		ar >> info >> ... ;
    		// Set groups back up
    	else {
    		// Serialize all runtime info
    		ar << info << ... ;
    		// Clear the groups vector
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)