Difference between revisions of "B2.Body:createFixture"

From GiderosMobile
(Created page with "__NOTOC__ '''Available since:''' Gideros 2011.6<br/> === Description === <br /> Creates a fixture and attach it to this body. If the density is non-zero, this function automat...")
 
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Class:''' [[b2.Body]]<br/>
 +
 
=== Description ===
 
=== Description ===
<br />
+
Creates a fixture and attach it to this body.
Creates a fixture and attach it to this body. If the density is non-zero, this function automatically<br />
+
<syntaxhighlight lang="lua">
updates the mass of the body. Contacts are not created until the next time step. The fixture definition is given<br />
+
(b2.Fixture) = b2.Body:createFixture(fixtureDef)
as a ordinary table. The fields of the fixture definition table are:<br />
+
</syntaxhighlight>
<br />
+
 
<ul><br />
+
The fixture definition is given as an ordinary table. The fields of the fixture definition table are:
<li>`shape`: (b2.Shape) The shape, this must be set.</li><br />
+
*'''shape''': (b2.Shape) the shape, this must be set
<li>`friction`: (number) The friction coefficient, usually in the range [0,1].</li><br />
+
*'''friction''': (number) the friction coefficient, usually in the range [0,1]
<li>`restitution`: (number) The restitution (elasticity) usually in the range [0,1].</li><br />
+
*'''restitution''': (number) the restitution (elasticity) usually in the range [0,1]
<li>`density`: (number) The density, usually in kg/m^2.</li><br />
+
*'''density''': (number) the density, usually in kg/m^2
<li>`isSensor`: (boolean) A sensor shape collects contact information but never generates a collision response.</li><br />
+
*'''isSensor''': (boolean) a sensor shape collects contact information but never generates a collision response
<li>`filter`: (table) Contact filtering data. The definition of contact filtering data is given at `b2.Fixture:setFilterData` function.</li><br />
+
*'''filter''': (table) contact filtering data. The definition of contact filtering data is given at [[b2.Fixture:setFilterData]] function
</ul><br />
+
 
<br />
+
If the density is non-zero, this function automatically updates the mass of the body. Contacts are not created until the next time step.
The unset fields gets default values.<br />
+
 
<br />
+
The unset fields get default values.
<ul><br />
+
 
<li>`Warning`: This function is locked during callbacks.</li><br />
+
'''This function is locked during callbacks'''
</ul><br />
+
<br />
+
'''Setting the friction a negative value will crash your game'''
<source lang="lua">
+
 
(b2.Fixture), = b2.Body:createFixture(fixtureDef,)
+
=== Parameters ===
</source>
+
'''fixtureDef''': (table) the fixture table definition<br/>
'''fixtureDef:''' (table) ''''''<br/>
+
 
'''Returns''' (b2.Fixture) The created fixture instance.<br/>
+
=== Return values ===
 +
'''Returns''' (b2.Fixture) the created fixture instance<br/>
 +
 
 +
=== Example ===
 +
<syntaxhighlight lang="lua">
 +
-- the body
 +
self.body = self.world:createBody{type = b2.DYNAMIC_BODY}
 +
self.body:setFixedRotation(true)
 +
self.body:setPosition(params.posx, params.posy)
 +
-- the shape
 +
self.w, self.h = self.bitmap:getWidth(), self.bitmap:getHeight()
 +
local playershape = b2.CircleShape.new(0, 0, self.w) -- (centerx, centery, radius)
 +
self.fixture = self.body:createFixture{
 +
shape = playershape, density = params.density, restitution = params.restitution, friction = params.friction,
 +
}
 +
</syntaxhighlight>
 +
 
 +
{{B2.Body}}

Latest revision as of 23:54, 30 April 2025

Available since: Gideros 2011.6
Class: b2.Body

Description

Creates a fixture and attach it to this body.

(b2.Fixture) = b2.Body:createFixture(fixtureDef)

The fixture definition is given as an ordinary table. The fields of the fixture definition table are:

  • shape: (b2.Shape) the shape, this must be set
  • friction: (number) the friction coefficient, usually in the range [0,1]
  • restitution: (number) the restitution (elasticity) usually in the range [0,1]
  • density: (number) the density, usually in kg/m^2
  • isSensor: (boolean) a sensor shape collects contact information but never generates a collision response
  • filter: (table) contact filtering data. The definition of contact filtering data is given at b2.Fixture:setFilterData function

If the density is non-zero, this function automatically updates the mass of the body. Contacts are not created until the next time step.

The unset fields get default values.

This function is locked during callbacks

Setting the friction a negative value will crash your game

Parameters

fixtureDef: (table) the fixture table definition

Return values

Returns (b2.Fixture) the created fixture instance

Example

-- the body
self.body = self.world:createBody{type = b2.DYNAMIC_BODY}
self.body:setFixedRotation(true)
self.body:setPosition(params.posx, params.posy)
-- the shape
self.w, self.h = self.bitmap:getWidth(), self.bitmap:getHeight()
local playershape = b2.CircleShape.new(0, 0, self.w) -- (centerx, centery, radius)
self.fixture = self.body:createFixture{
	shape = playershape, density = params.density, restitution = params.restitution, friction = params.friction,
}




LiquidFun