Skip to content

Commit

Permalink
Merge pull request #4 from mitchwadair/development
Browse files Browse the repository at this point in the history
Add friction to physics
  • Loading branch information
mitchwadair authored Mar 9, 2020
2 parents c908011 + eaeff6b commit 78d9cbc
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 15 deletions.
Binary file added build/SFramework_v0.3.2.cspack
Binary file not shown.
21 changes: 20 additions & 1 deletion doc/API/Components/Physics.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Physics component handles object physics.
- [SetStatic](#physicssetstatic)
- [GetColliderType](#physicsgetcollidertype)
- [SetColliderType](#physicssetcollidertype)
- [GetFriction](#physicsgetfriction)
- [SetFriction](#physicssetfriction)
- [ApplyImpulse](#physicsapplyimpulse)

#Physics
Expand All @@ -31,6 +33,7 @@ Physics.bounciness
Physics.velocity
Physics.static
Physics.colliderType
Physics.friction
```

## Physics:GetMass
Expand Down Expand Up @@ -137,14 +140,30 @@ local myCollider = self.gameObject.physics:GetColliderType()
```

## Physics:SetColliderType
Sets the velocity of the Physics object to the given vector
Sets the collider type of the Physics object to the value
### Arguments
- `type` - `string` (required) the collider type to set to
### Example
```lua
self.gameObject.physics:SetColliderType('CIRCLE')
```

## Physics:GetFriction
Returns the friction coefficient of the object
### Example
```lua
local myFrictionCoefficient = self.gameObject.physics:GetFriction()
```

## Physics:SetFriction
Sets the friction coefficient of the Physics object to the given value. This should be a value between 0 and 1
### Arguments
- `type` - `number` (required) the friction coefficient to set to
### Example
```lua
self.gameObject.physics:SetFriction(.5)
```

## Physics:ApplyImpulse
Applies an impulse to the Physics object according to the given vector
### Arguments
Expand Down
6 changes: 4 additions & 2 deletions doc/Tutorials/adding_physics_to_a_gameobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ First, select your game object, and add a new scripted behavior
## Setting Values
The default values for the Physics component are good, but in some cases, you might need to change the values to fit your game object. Most commonly you'll need to change the collider's width and height, and possibly the collider shape too.

![](https://i.imgur.com/c1xwtvZ.png)
**NOTE: If you are using a circle collider, the `height` value will not be considered**

![](https://i.imgur.com/Q4IzV4j.png)

# Visualizing the Collider
It is not very apparent when adding Physics to your object what your collider will look like. To visualize it, you can use SFramework's sample box, triangle, and circle sprites. Just add a child game object and center it on your game object. Then, add a TextRenderer using the correct collider sprite (make sure the text value is ' '!) and adjust the child object's scale to fit your sprite. These stretch values are what you will use as the width and height of your Physics component.
It is not very apparent when adding Physics to your object what your collider will look like. To visualize it, you can use SFramework's sample box, triangle, and circle sprites. Just add a child game object and center it on your game object. Then, add a TextRenderer using the correct collider sprite and adjust the child object's scale to fit your sprite. These stretch values are what you will use as the width and height of your Physics component. Make sure you are using a space (' ') as your TextRenderer's text value to display the collider visualizer correctly.

![](https://i.imgur.com/4DMW0tm.png)
7 changes: 6 additions & 1 deletion src/SFramework/CS API Extensions/GameObject.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

--[[
Gets the sprite renderer from the calling GameObject
Expand Down
7 changes: 7 additions & 0 deletions src/SFramework/Components/MapCollider.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

function Behavior:Awake()
local offset = {
x = self.gameObject.transform:GetPosition().x,
Expand Down
33 changes: 32 additions & 1 deletion src/SFramework/Components/Physics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ mass number 1
static boolean False
bounciness number 0
colliderType string "BOX"
friction number 1
/PublicProperties]]
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

function Behavior:Awake()
-- initialize physics managaer if it is not present
Expand All @@ -25,6 +31,7 @@ function Behavior:Awake()
if not self.static and self.mass > 0 then self.inverseMass = 1/self.mass end

self.colliderType = string.upper(self.colliderType) --make it more robust
self.friction = math.clamp(self.friction, 0, 1) --ensure a value between 0 and 1

self.velocity = Vector3:New(0, 0, 0)
end
Expand Down Expand Up @@ -209,6 +216,30 @@ function Behavior:SetColliderType(type)
self.colliderType = upperCase
end

--[[
Returns the physics object's friction coefficient
@return the friction coefficient of the physics object
]]--
function Behavior:GetFriction()
return self.friction
end

--[[
Sets the physics object's friction coefficient to the given value
@param friction the friction coefficient to set to
]]--
function Behavior:SetFriction(friction)
if friction == nil then
error("missing required argument 'friction' in SetFriction call")
return
end
local f = math.clamp(math.abs(friction), 0, 1)

self.friction = f
end

function Behavior:Update()
local manager = SF.physics.manager
if not self.static then
Expand Down
7 changes: 6 additions & 1 deletion src/SFramework/Components/SpriteRenderer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

function Behavior:Awake()
--set defaults
Expand Down
15 changes: 12 additions & 3 deletions src/SFramework/Engines/SF Physics Manager.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

function Behavior:Awake()
self.physicsObjects = {}
Expand Down Expand Up @@ -275,10 +280,14 @@ function Behavior:ResolveCollision(obj1, obj2, data)
impulseMagnitude = impulseMagnitude / (obj1.physics.inverseMass + obj2.physics.inverseMass)

local impulse = data.normal * impulseMagnitude
obj1.physics.velocity = obj1.physics.velocity + (obj1.physics.inverseMass*impulse)
local frictionCoeff = obj1.physics.friction * obj2.physics.friction
local friction = Vector3:New(data.normal.y * frictionCoeff, data.normal.x * frictionCoeff, 0)/60
obj1.physics.velocity = obj1.physics.velocity + (obj1.physics.inverseMass*impulse) - (friction*obj1.physics.inverseMass*Vector3:New(math.sign(obj1.physics.velocity.x), math.sign(obj1.physics.velocity.y), 0))
if math.abs(obj1.physics.velocity.y) <= .05 * SF.physics.gravity then obj1.physics.velocity.y = 0 end
obj2.physics.velocity = obj2.physics.velocity - (obj2.physics.inverseMass*impulse)
if math.abs(obj1.physics.velocity.x) <= .02 then obj1.physics.velocity.x = 0 end
obj2.physics.velocity = obj2.physics.velocity - (obj2.physics.inverseMass*impulse) + (friction*obj2.physics.inverseMass*Vector3:New(math.sign(obj2.physics.velocity.x), math.sign(obj2.physics.velocity.y), 0))
if math.abs(obj2.physics.velocity.y) <= .05 * SF.physics.gravity then obj2.physics.velocity.y = 0 end
if math.abs(obj2.physics.velocity.x) <= .02 then obj2.physics.velocity.x = 0 end

obj1.transform:SetPosition(obj1.transform:GetPosition() + (data.normal * data.depth))
obj1.transform:SetPosition(obj1.transform:GetPosition() + Vector3:New(0, .0002, 0))
Expand Down
7 changes: 6 additions & 1 deletion src/SFramework/LUA Extensions/math.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

--[[
Returns the sign of the given value
Expand Down
7 changes: 6 additions & 1 deletion src/SFramework/LUA Extensions/table.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

--[[
Returns the index of a given object in the given table
Expand Down
7 changes: 6 additions & 1 deletion src/SFramework/SFramework.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

SF = {}

Expand Down
7 changes: 6 additions & 1 deletion src/SFramework/Tables.lua
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
--[[ Made by Mitchell Adair https://github.com/mitchwadair ]]--
-- Copyright (c) 2020 Mitchell Adair
--
-- (https://github.com/mitchwadair)
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT
5 changes: 3 additions & 2 deletions src/TestScript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ function Behavior:Awake()
end

function Behavior:Start()
self.gameObject.physics:ApplyImpulse(Vector3:New(-.2, 0, 0))
self.gameObject.physics:ApplyImpulse(Vector3:New(0, 0, 0))
end

function Behavior:Update()
--self.gameObject.physics.velocity.x = .1
--self.gameObject.physics.velocity.x = -.1
--print(self.gameObject.physics.velocity)
end

0 comments on commit 78d9cbc

Please sign in to comment.