Difference between revisions of "B2.Body:createFixture"

From GiderosMobile
m (Text replacement - "</source" to "</syntaxhighlight")
 
(One intermediate revision by one other user not shown)
Line 4: Line 4:
  
 
=== Description ===
 
=== Description ===
Creates a fixture and attach it to this body. If the density is non-zero, this function automatically
+
Creates a fixture and attach it to this body.
updates the mass of the body. Contacts are not created until the next time step. The fixture definition is given as an ordinary table. The fields of the fixture definition table are:
+
<syntaxhighlight lang="lua">
 +
(b2.Fixture) = b2.Body:createFixture(fixtureDef)
 +
</syntaxhighlight>
  
 +
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
 
*'''shape''': (b2.Shape) the shape, this must be set
 
*'''friction''': (number) the friction coefficient, usually in the range [0,1]
 
*'''friction''': (number) the friction coefficient, usually in the range [0,1]
Line 13: Line 16:
 
*'''isSensor''': (boolean) a sensor shape collects contact information but never generates a collision response
 
*'''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
 
*'''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.
 
The unset fields get default values.
  
<source lang="lua">
+
'''This function is locked during callbacks'''
(b2.Fixture) = b2.Body:createFixture(fixtureDef)
+
</syntaxhighlight>
+
'''Setting the friction a negative value will crash your game'''
 
 
'''Warning''': this function is locked during callbacks.
 
 
 
'''Warning''': setting the friction a negative value will crash your game.
 
  
 
=== Parameters ===
 
=== Parameters ===
'''fixtureDef''': (table) <br/>
+
'''fixtureDef''': (table) the fixture table definition<br/>
  
 
=== Return values ===
 
=== Return values ===
Line 31: Line 32:
  
 
=== Example ===
 
=== Example ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
-- the body
 
-- the body
 
self.body = self.world:createBody{type = b2.DYNAMIC_BODY}
 
self.body = self.world:createBody{type = b2.DYNAMIC_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