GenerateCollisionMaskFromTexture() Questions

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Sun Nov 15, 2009 3:04 am

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?
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sun Nov 15, 2009 3:05 am

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?
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Sun Nov 15, 2009 4:07 am

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():

[code:3piu01s7]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;
}[/code:3piu01s7]
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);

...

ar.detach();

...

// Update the object's collision mask
pRuntime->UpdateBoundingBox(this);
pRuntime->GenerateCollisionMaskFromTexture(this, iTextureD);

...
}[/code:3piu01s7]
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.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sun Nov 15, 2009 3:23 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Sun Nov 15, 2009 6:07 pm

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?
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sun Nov 15, 2009 6:44 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Sun Nov 15, 2009 8:05 pm

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):

[code:wkw8e52u]void ExtObject::Serialize(bin& ar)
{
if (ar.loading) {

// Serialize all runtime info
ar >> info >> ... ;

// Set groups back up
SetupGroup(0);

}
else {

// Serialize all runtime info
ar << info << ... ;

// Clear the groups vector
groups.clear();

}
}[/code:wkw8e52u]
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests