碰撞事件对应捕捉与处理实例
来源:互联网 发布:杭州友谦网络 编辑:程序博客网 时间:2024/06/07 11:17
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
- 碰撞事件对应捕捉与处理实例
- 事件冒泡与事件捕捉
- 碰撞与触发事件
- VC程序异常处理,异常日志生成,及与源代码对应,异常捕捉
- ReactNative错误捕捉与处理
- Android 事件捕捉和处理流程分析
- Android 事件捕捉和处理流程分析
- Android 事件捕捉和处理流程分析
- 捕捉C#全局异常的事件处理
- Android 事件捕捉和处理流程
- Qt捕捉窗口关闭事件与信号的捕捉
- Qt捕捉窗口关闭事件与信号的捕捉
- Qt捕捉窗口关闭事件与信号的捕捉
- Qt捕捉窗口关闭事件与信号的捕捉
- 捕捉网页关闭与取消关闭事件
- qt焦点事件响应捕捉处理,事件过滤器
- 鼠标碰撞检测与事件监听
- react-native 错误捕捉与处理
- postgres 存储过程 游标 函数
- N-vop、S-vop、Packed Bistream
- LOESS资料总结
- 二叉树的中序遍历的非递归算法
- Linux下chkconfig命令详解
- 碰撞事件对应捕捉与处理实例
- 计算机SAP-1 设计与实现(1)
- 开源 免费 java CMS - FreeCMS-功能说明-信息管理
- 给软件开发初学者 c++
- SQL 执行顺序和性能优化
- Python集成开发环境(eclipse+pydev)遇到的问题
- Linux系统目录结构
- 用Java写算法技巧(1)去掉List中的重复对象
- MPMoviePlayerController 获取视频缓冲大小