gpt4 book ai didi

ios - ApplyImpulse 对插入物体的力度具有指数效应

转载 作者:行者123 更新时间:2023-11-30 11:58:58 24 4
gpt4 key购买 nike

在这个简单的物理模拟中存在一些奇怪的行为。 applyImpulse 每 5 秒应用一次,但每次发生这种情况时,即使 applyImpulse 使用相同的值,该框也会跳得更高。大约20秒后,盒子升得很高。

盒子上附有一个红色小盒子,这是施加脉冲的地方。

拍摄的照片:

这是代码:

import SpriteKit

enum collisionType: UInt32 {
case obj = 0b1 //1
case bottom = 0b10 //2
}

var cam: SKCameraNode?
var screenSize = CGSize()

class GameScene: SKScene, SKPhysicsContactDelegate {

let obj = SKSpriteNode(color: .white, size: CGSize(width: 50, height: 50))
var camBottom = SKSpriteNode()
let block = SKSpriteNode(color: .red, size: CGSize(width: 25, height: 25))

然后在重写 func didMove(to view: SKView):

    screenSize = self.size
camBottom = SKSpriteNode(color: .gray, size: CGSize(width: ((3/2) * screenSize.width), height: 200))
physicsWorld.contactDelegate = self

cam = SKCameraNode()
self.camera = cam
addChild(cam!)

camBottom.position.y = -screenSize.height/2 + camBottom.size.height/2
camBottom.zPosition = -1
addChild(camBottom)
camBottom.addPhysicsBody(category: .bottom, collision: nil, contact: .obj, isDynamic: false, gravity: false, rotation: false)
camBottom.physicsBody?.restitution = 0.0
camBottom.physicsBody?.isResting = true
camBottom.physicsBody?.friction = 1.0

addChild(obj)
block.position = CGPoint(x: -30, y: -obj.size.height/2)
obj.addChild(block)

obj.physicsBody = SKPhysicsBody(bodies: [SKPhysicsBody(rectangleOf: obj.size, center: obj.position), SKPhysicsBody(rectangleOf: block.size, center: block.position)])
obj.physicsBody?.categoryBitMask = collisionType.obj.rawValue
obj.physicsBody?.collisionBitMask = collisionType.bottom.rawValue
obj.physicsBody?.contactTestBitMask = collisionType.bottom.rawValue
obj.physicsBody?.affectedByGravity = true
obj.physicsBody?.allowsRotation = true

runAutomation()

let wait = SKAction.wait(forDuration: 0.01)
let act = SKAction.run {
self.updateCam()
}
run(SKAction.repeatForever(SKAction.sequence([wait, act])))

.addPhysicsBody是一个新的Swift文件中定义的扩展,它是这样的:

extension SKSpriteNode {
func addPhysicsBody(category: collisionType, collision: collisionType?, contact: collisionType?, isDynamic: Bool, gravity: Bool, rotation: Bool) {
self.physicsBody = SKPhysicsBody(rectangleOf: self.size)
self.physicsBody?.categoryBitMask = category.rawValue
if collision != nil {
self.physicsBody?.collisionBitMask = (collision?.rawValue)!
} else {
self.physicsBody?.collisionBitMask = 0
}
if contact != nil {
self.physicsBody?.contactTestBitMask = (contact?.rawValue)!
} else {
self.physicsBody?.contactTestBitMask = 0
}
self.physicsBody?.isDynamic = isDynamic
self.physicsBody?.affectedByGravity = gravity
self.physicsBody?.allowsRotation = rotation
}
}

然后是GameScene中定义的函数:

func updateCam() {
let val: CGFloat = 15
let xDist = (obj.position.x - (camera?.position.x)!)/val
let yDist = (obj.position.y - (camera?.position.y)!)/val
camera?.position.x += xDist
camera?.position.y += yDist
}

func runAutomation() {
let wait = SKAction.wait(forDuration: 5.0)
let act = SKAction.run {
print("Before", self.obj.physicsBody?.velocity)
self.obj.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30), at: self.block.position)
print("After", self.obj.physicsBody?.velocity)
}
run(SKAction.repeatForever(SKAction.sequence([wait, act])))
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
camBottom.position.x = (camera?.position.x)!
}

很奇怪,不知道是不是故意的。然而,我怎样才能使每次施加脉冲时,盒子飞起大约相同的距离?正确的方法是什么?

谢谢

最佳答案

看起来您正在应用来自 self.block.position 的脉冲

self.obj.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30), at: self.block.position)

在某些时候,您的对象并不像以前那样位于同一位置。因此它将从另一个位置施加相同的脉冲。也许您想要 self.obj.position 而不是 self.block.position 或只使用

self.obj.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))

为了让它跳得更高,你将 dy 设置为更高的值。

关于ios - ApplyImpulse 对插入物体的力度具有指数效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com