碰撞事件对应捕捉与处理实例
2012-12-26 14:02
309 查看
function love.load()
--create a new world
world = love.physics.newWorld(0,200,true)
--These callback function names can be almost any you want:
world:setCallbacks(beginContact,endContact,preSolve,postSolve)
-- we'll use this to put info text on the screen later
text = ""
-- we'll use this to store the state of repeated callback calls
persisting = 0
--create a ball with bouncy
ball = {}
ball.b = love.physics.newBody(world, 400,50,"dynamic")
ball.b:setMass(10)
ball.s = love.physics.newCircleShape(20)
ball.f = love.physics.newFixture(ball.b,ball.s)
ball.f:setRestitution(0.4)
ball.f:setUserData("BALL")
--
static = {}
static.b = love.physics.newBody(world,400,400,"static")
static.s = love.physics.newRectangleShape(200,50)
static.f = love.physics.newFixture(static.b,static.s)
static.f:setUserData("STATIC")
end
function love.update(dt)
--let the world have the update event
world:update(dt)
if love.keyboard.isDown("right") then
ball.b:applyForce(1000, 0)
elseif love.keyboard.isDown("left") then
ball.b:applyForce(-1000, 0)
end
if love.keyboard.isDown("up") then
ball.b:applyForce(0, -5000)
elseif love.keyboard.isDown("down") then
ball.b:applyForce(0, 1000)
end
if string.len(text) > 768 then -- cleanup when 'text' gets too long
text = ""
end
end
function love.draw()
--画圆
love.graphics.setColor(255,0,0)
love.graphics.circle("fill", ball.b:getX(),ball.b:getY(), ball.s:getRadius(), 20)
--画矩形
love.graphics.setColor(0,255,0)
love.graphics.polygon("fill", static.b:getWorldPoints(static.s:getPoints()))
love.graphics.print(text, 10, 10)
end
--a is the first fixture object in the collision.
--b is the second fixture object in the collision.
--coll is the contact object created.
function beginContact(a,b,coll)
x,y = coll:getNormal()
text = text.."\n"..a:getUserData().." crashing with "..b:getUserData().." with a vector normal of: "..x..", "..y
end
function endContact(a,b,coll)
persisting = 0 -- reset since they're no longer touching
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function preSolve(a,b,coll)
-- only say when they first start touching
if persisting == 0 then
text = text.."\n"..a:getUserData().." touchin-- only say when they first start touchingg "..b:getUserData()
elseif persisting < 20 then -- then just start counting
text = text.." "..persisting
end
persisting = persisting + 1 -- keep track of how many updates they've been touching for
end
function postSolve(a,b,coll)
end
--create a new world
world = love.physics.newWorld(0,200,true)
--These callback function names can be almost any you want:
world:setCallbacks(beginContact,endContact,preSolve,postSolve)
-- we'll use this to put info text on the screen later
text = ""
-- we'll use this to store the state of repeated callback calls
persisting = 0
--create a ball with bouncy
ball = {}
ball.b = love.physics.newBody(world, 400,50,"dynamic")
ball.b:setMass(10)
ball.s = love.physics.newCircleShape(20)
ball.f = love.physics.newFixture(ball.b,ball.s)
ball.f:setRestitution(0.4)
ball.f:setUserData("BALL")
--
static = {}
static.b = love.physics.newBody(world,400,400,"static")
static.s = love.physics.newRectangleShape(200,50)
static.f = love.physics.newFixture(static.b,static.s)
static.f:setUserData("STATIC")
end
function love.update(dt)
--let the world have the update event
world:update(dt)
if love.keyboard.isDown("right") then
ball.b:applyForce(1000, 0)
elseif love.keyboard.isDown("left") then
ball.b:applyForce(-1000, 0)
end
if love.keyboard.isDown("up") then
ball.b:applyForce(0, -5000)
elseif love.keyboard.isDown("down") then
ball.b:applyForce(0, 1000)
end
if string.len(text) > 768 then -- cleanup when 'text' gets too long
text = ""
end
end
function love.draw()
--画圆
love.graphics.setColor(255,0,0)
love.graphics.circle("fill", ball.b:getX(),ball.b:getY(), ball.s:getRadius(), 20)
--画矩形
love.graphics.setColor(0,255,0)
love.graphics.polygon("fill", static.b:getWorldPoints(static.s:getPoints()))
love.graphics.print(text, 10, 10)
end
--a is the first fixture object in the collision.
--b is the second fixture object in the collision.
--coll is the contact object created.
function beginContact(a,b,coll)
x,y = coll:getNormal()
text = text.."\n"..a:getUserData().." crashing with "..b:getUserData().." with a vector normal of: "..x..", "..y
end
function endContact(a,b,coll)
persisting = 0 -- reset since they're no longer touching
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function preSolve(a,b,coll)
-- only say when they first start touching
if persisting == 0 then
text = text.."\n"..a:getUserData().." touchin-- only say when they first start touchingg "..b:getUserData()
elseif persisting < 20 then -- then just start counting
text = text.." "..persisting
end
persisting = persisting + 1 -- keep track of how many updates they've been touching for
end
function postSolve(a,b,coll)
end
相关文章推荐
- ABAP面向对象事件处理-简单实例
- iOS的界面触摸事件处理机制,然后用一个实例来说明下应用场景.
- C#控制台程序中处理2个关闭事件的代码实例
- kinect编程(二):添加事件处理,捕捉数据
- Android事件处理机制实例讲解
- 关于委托、事件、处理事件的方法以及.NET 续一个实例
- Android 事件捕捉和处理流程分析
- jQuery源码学习(版本1.11)-事件处理-实例函数
- javascript事件处理模型实例说明
- Qt中事件处理的方法和实例
- 【Linux 开发】Libevent事件处理机制及实例
- Asp.Net 利用反射获得委托和事件以及创建委托实例和添加事件处理程序
- Qt5事件处理及实例
- 事件处理基础 处理按钮点击事件实例 Java核心技术
- android dialog在activity中,如何处理activity捕捉不到back点击事件
- C#控制台程序中处理2个关闭事件的代码实例
- 微信小程序实例:image组件的binderror事件处理
- UITouch 触摸事件处理(实例)
- MyBatis入门实例-包括实体类与数据库字段对应&CLOB字段处理
- C#控制台程序中处理2个关闭事件的代码实例