2
고정 된 물체에 센서를 추가하여 움직이는 물체를 탐지하려고합니다. box1
은 고정되어 있으며 큰 원형 센서를 가지고 있으며 box2
은 운동 학적이며 선 속도를 설정하여 이동합니다.운동기구 대 동적 설비와의 센서 접촉
센서는 이동 물체를 검출하지 않는다 :
import QtQuick 2.6
import QtQuick.Window 2.2
import Box2D 2.0 as Box2D
Window {
id: window
width: 640
height: 480
visible: true
Box2D.World {
id: physicsWorld
gravity: Qt.point(0, 0)
}
Item {
id: box1
x: window.width/2 - width/2
y: window.height/2 - height/2
width: 32
height: 32
property int sensorRadius: 128
Box2D.Body {
id: boxBody
target: box1
fixtures: [
Box2D.Box {
width: box1.width
height: box1.height
},
Box2D.Circle {
x: box1.width/2 - box1.sensorRadius
y: box1.height/2 - box1.sensorRadius
objectName: boxBody.objectName + "CircleSensor"
radius: box1.sensorRadius
sensor: true
onBeginContact: touchIndicator.border.color = "red"
onEndContact: touchIndicator.border.color = "transparent"
}
]
}
Rectangle {
id: touchIndicator
anchors.centerIn: parent
width: box1.sensorRadius * 2
height: box1.sensorRadius * 2
color: "transparent"
border.color: "transparent"
}
}
Item {
id: box2
x: 100
y: 160
width: 32
height: 32
focus: true
Keys.onSpacePressed: box2Body.linearVelocity = Qt.point(3, 0)
Box2D.Body {
id: box2Body
world: physicsWorld
target: box2
bodyType: Box2D.Body.Kinematic
Box2D.Box {
width: box2.width
height: box2.height
}
}
}
Box2D.DebugDraw {
id: debugDraw
world: physicsWorld
anchors.fill: parent
opacity: 0.75
}
}
이
의 결과이다.나는 이동 객체가 동적으로 만들 경우
이bodyType: Box2D.Body.Dynamic
는 다음을 감지합니다
이 작업을 수행하는 올바른 방법은 무엇입니까?
것을 명심하십시오
- 가 나는 힘 (따라서 내 현재
linearVelocity
와 접근 방법과Kinematic
를 사용하고자하는)를 통해 정확한 움직임을 빨아 때문에 운동을 간단하게 유지하려는. - 나는 실제로 충돌하고 다른 것을 튀길 필요가 없기를 바랄뿐입니다. 센서와 레이 캐스팅이 필요합니다.
운동기구에는 질량이 없으므로 충돌 감지에 관여하지 않습니다. Box2D는 수신 거부와 그 모든 것을 계산할 수 없습니다. 운동기구는 주로 정적 구조를 대상으로합니다. – folibis
http://www.iforce2d.net/b2dtut/bodies는 운동기구가 움직일 수 있다고 말하고 [this] (http://www.emanueleferonato.com/2012/05/11/understanding-box2d-kinematic -bodies /) 페이지가 동적 객체와 충돌하는 것을 보여줍니다. 그걸 사용하려고하는 방식으로 사용할 수 없다고 확신합니까? – Mitch
예, 운동기구는 힘의 영향을받지 않습니다. Box2D는 뉴튼 물리학을 구현하므로 질량이 무한한 물체 (예 : 운동 학적 물체)는 충돌 감지에 관여 할 수 없습니다. 충돌을 피하는 것이 목표라면 (센서처럼 행동하는 것), 예제 중 하나에서와 같이'Fixture.categories' 및'Fixture.collidesWith'로 재생할 수 있습니다. 나는 그들 중 누구인지 기억하지 못한다. – folibis