2013-11-28 7 views
7

Frist बंद, धन्यवाद आप प्रारंभिक वाहन कोड के लिए @Smick यहां पोस्ट के रूप में: Sprite Kit pin joints appear to have an incorrect anchorस्प्राइट किट - स्प्रिंग जोड़ों (झटका अवशोषक)

मैं पहिया के बीच एक स्लाइड संयुक्त जोड़ने के लिए कोशिश कर रहा हूँ (बाएं पहिया) और एक शॉक अवशोषण प्रभाव बनाने के लिए एक वसंत संयुक्त के साथ चेसिस।

नीचे दिए गए मेरे कोड के साथ, मुझे कोई संपीड़न नहीं मिलता है। मुझे एहसास है कि प्रलेखन एक वसंत संयुक्त को दो बोड खींचकर दिखाता है - जो मैं चाहता हूं उसके विपरीत। एसके में यह संभव है?

मुझे लगता है कि पिन संयुक्त अपराधी हो सकता है? जब मैं पिन संयुक्त को टिप्पणी करता हूं, तो कार के हिस्सों में गड़बड़ी होती है - सब कुछ स्क्रीन के चारों ओर उड़ता है। मूल रूप से, पिन संयुक्त ने चेसिस को पहिया पिन किया, लेकिन जाहिर है कि मैं पहिया को "सदमे अवशोषक" में पिन करना चाहता हूं।

इसके अलावा, SKPhysicsJointSliding के लिए "अक्ष" तर्क मुझे थोड़ा उलझन में डाल दिया है। यह एक वेक्टर चाहता है। एक वेक्टर के सापेक्ष?

अग्रिम धन्यवाद।

- (SKShapeNode*) makeWheel 
{ 
SKShapeNode *wheel = [[SKShapeNode alloc] init]; 
CGMutablePathRef myPath = CGPathCreateMutable(); 
CGPathAddArc(myPath, NULL, 0,0, 16, 0, M_PI*2, YES); 
wheel.path = myPath; 
wheel.physicsBody.mass = 0.5; 
return wheel; 
} 

- (void) createCar{ 

// 1. car body 
SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)]; 
carBody.position = CGPointMake(200, 700); 
carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size]; 
carBody.physicsBody.mass = 1.0; 
[self addChild:carBody]; 

// 2. wheels 
SKShapeNode *leftWheel = [self makeWheel]; 
leftWheel.position = CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y-40); 
leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16]; 
[self addChild:leftWheel]; 

SKShapeNode *rightWheel = [self makeWheel]; 
rightWheel.position = CGPointMake(carBody.position.x + carBody.size.width/2, carBody.position.y); 
rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16]; 
[self addChild:rightWheel]; 


/* Build left shock absorber and attach wheel */ 

CGVector av =CGVectorMake(0.0, 5.0); 

SKPhysicsJointSliding *leftSlide = [SKPhysicsJointSliding jointWithBodyA:carBody.physicsBody 
                    bodyB:leftWheel.physicsBody 
                    anchor:leftWheel.position 
                    axis:av]; 


SKPhysicsJointSpring *leftSpring = [SKPhysicsJointSpring jointWithBodyA:carBody.physicsBody bodyB:leftWheel.physicsBody 
                   anchorA:CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y) 
                  anchorB:leftWheel.position]; 



SKPhysicsJointPin *leftPin = [SKPhysicsJointPin jointWithBodyA:leftSpring.bodyA 
                 bodyB:leftSpring.bodyB 
                 anchor:leftWheel.position]; 


[self.physicsWorld addJoint:leftSlide]; 
[self.physicsWorld addJoint:leftSpring]; 
[self.physicsWorld addJoint:leftPin]; 

[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody  bodyB:rightWheel.physicsBody anchor:rightWheel.position]]; 

}

उत्तर

8

मेरा उत्तर का संपादन। निलंबन के लिए स्लाइड्स संयुक्त के माध्यम से पहियों को जोड़कर स्लाइडिंग बॉडी से पहियों को जोड़ा जाना आवश्यक है। पूर्व करने से पहियों को घूमने की अनुमति मिलती है। उत्तरार्द्ध नहीं करता है। जो मैं SpriteKit के साथ काम करने के लिए अमूल्य लगता है -

Vehicle.m

#import "Vehicle.h" 

@implementation Vehicle 

- (SKSpriteNode*) makeWheel 
{ 
    SKSpriteNode *wheel = [SKSpriteNode spriteNodeWithImageNamed:@"wheel.png"]; 
// wheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:wheel.size.width/2]; 
    return wheel; 
} 

-(id)initWithPosition:(CGPoint)pos { 

    if (self = [super init]) { 

     _joints = [NSMutableArray array]; 

     int wheelOffsetY = 60; 
     CGFloat damping  = 1; 
     CGFloat frequency = 4; 

     SKSpriteNode *chassis = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)]; 
     chassis.position = pos; 
     chassis.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:chassis.size]; 
     [self addChild:chassis]; 

     _ctop = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(70, 16)]; 
     _ctop.position = CGPointMake(chassis.position.x+20, chassis.position.y+12); 
     _ctop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ctop.size]; 
     [self addChild:_ctop]; 

     SKPhysicsJointFixed *cJoint = [SKPhysicsJointFixed jointWithBodyA:chassis.physicsBody 
                    bodyB:_ctop.physicsBody 
                    anchor:CGPointMake(_ctop.position.x, _ctop.position.y)]; 


     _leftWheel = [self makeWheel]; 
     _leftWheel.position = CGPointMake(chassis.position.x - chassis.size.width/2, chassis.position.y - wheelOffsetY); //Always set position before physicsBody 
     _leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_leftWheel.size.width/2]; 
     _leftWheel.physicsBody.allowsRotation = YES; 
     [self addChild:_leftWheel]; 

     SKSpriteNode *rightWheel = [self makeWheel]; 
     rightWheel.position = CGPointMake(chassis.position.x + chassis.size.width/2, chassis.position.y - wheelOffsetY); 
     rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:rightWheel.size.width/2]; 
     rightWheel.physicsBody.allowsRotation = YES; 
     [self addChild:rightWheel]; 

//------------- LEFT SUSPENSION ----------------------------------------------------------------------------------------------- // 

     SKSpriteNode *leftShockPost = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(7, wheelOffsetY)]; 
     leftShockPost.position = CGPointMake(chassis.position.x - chassis.size.width/2, chassis.position.y - leftShockPost.size.height/2); 
     leftShockPost.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:leftShockPost.size]; 
     [self addChild:leftShockPost]; 

     SKPhysicsJointSliding *leftSlide = [SKPhysicsJointSliding jointWithBodyA:chassis.physicsBody 
                      bodyB:leftShockPost.physicsBody 
                anchor:CGPointMake(leftShockPost.position.x, leftShockPost.position.y) 
                 axis:CGVectorMake(0, 1)]; 

     leftSlide.shouldEnableLimits = TRUE; 
     leftSlide.lowerDistanceLimit = 5; 
     leftSlide.upperDistanceLimit = wheelOffsetY; 


     SKPhysicsJointSpring *leftSpring = [SKPhysicsJointSpring jointWithBodyA:chassis.physicsBody bodyB:_leftWheel.physicsBody 
                     anchorA:CGPointMake(chassis.position.x - chassis.size.width/2, chassis.position.y) 
                     anchorB:_leftWheel.position]; 
     leftSpring.damping = damping; 
     leftSpring.frequency = frequency; 

     SKPhysicsJointPin *lPin = [SKPhysicsJointPin jointWithBodyA:leftShockPost.physicsBody bodyB:_leftWheel.physicsBody anchor:_leftWheel.position]; 


//------------- RIGHT SUSPENSION ----------------------------------------------------------------------------------------------- // 

     SKSpriteNode *rightShockPost = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(7, wheelOffsetY)]; 
     rightShockPost.position = CGPointMake(chassis.position.x + chassis.size.width/2, chassis.position.y - rightShockPost.size.height/2); 
     rightShockPost.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rightShockPost.size]; 
     [self addChild:rightShockPost]; 

     SKPhysicsJointSliding *rightSlide = [SKPhysicsJointSliding jointWithBodyA:chassis.physicsBody 
                      bodyB:rightShockPost.physicsBody 
                      anchor:CGPointMake(rightShockPost.position.x, rightShockPost.position.y) 
                      axis:CGVectorMake(0, 1)]; 

     rightSlide.shouldEnableLimits = TRUE; 
     rightSlide.lowerDistanceLimit = 5; 
     rightSlide.upperDistanceLimit = wheelOffsetY; 


     SKPhysicsJointSpring *rightSpring = [SKPhysicsJointSpring jointWithBodyA:chassis.physicsBody bodyB:rightWheel.physicsBody 
                     anchorA:CGPointMake(chassis.position.x + chassis.size.width/2, chassis.position.y) 
                     anchorB:rightWheel.position]; 
     rightSpring.damping = damping; 
     rightSpring.frequency = frequency; 

     SKPhysicsJointPin *rPin = [SKPhysicsJointPin jointWithBodyA:rightShockPost.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position]; 


     // Add all joints to the array. 

     [_joints addObject:cJoint]; 

     [_joints addObject:leftSlide]; 
     [_joints addObject:leftSpring]; 
     [_joints addObject:lPin]; 

     [_joints addObject:rightSlide]; 
     [_joints addObject:rightSpring]; 
     [_joints addObject:rPin]; 

    } 

    return self; 
} 


@end 
3

मैं एक खेल के मैदान में परीक्षण के लिए तेज में यिर्मयाह के कोड में परिवर्तित करने की स्वतंत्रता ले लिया। कोई नई कार्यक्षमता नहीं; बस एक खेल के मैदान में चलाने के लिए स्विफ्ट और संशोधित करने के लिए अनुवादित:

अब स्विफ्ट 3 के लिए अपडेट किया गया है। क्योंकि मुझे परवाह है।

import Foundation 
import SpriteKit 
import PlaygroundSupport 

let view:SKView = SKView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768)) 
PlaygroundPage.current.liveView = view 

let scene = SKScene(size: CGSize(width: 1024, height: 768)) 
scene.name = "PlaygroundScene" 
scene.physicsWorld.gravity = CGVector() 
scene.scaleMode = SKSceneScaleMode.aspectFill 

var vehicle = SKNode() 

var joints = [SKPhysicsJoint]() 
let wheelOffsetY:CGFloat = 60; 
let damping:CGFloat  = 1; 
let frequency :CGFloat = 4; 

let chassis = SKSpriteNode.init(color: UIColor.white, size: CGSize(width: 120, height: 8)) 
chassis.position = CGPoint(x: scene.size.width/2, y: scene.size.height/2) 
chassis.physicsBody = SKPhysicsBody.init(rectangleOf: chassis.size) 
vehicle.addChild(chassis) 


let ctop = SKSpriteNode.init(color: UIColor.green, size: CGSize(width: 70, height: 16)) 

ctop.position = CGPoint(x: chassis.position.x+20, y: chassis.position.y+12) 
ctop.physicsBody = SKPhysicsBody.init(rectangleOf: ctop.size) 
vehicle.addChild(ctop) 



let cJoint = SKPhysicsJointFixed.joint(withBodyA: chassis.physicsBody!, bodyB: ctop.physicsBody!, anchor: CGPoint(x: ctop.position.x, y: ctop.position.y)) 



let leftWheel = SKSpriteNode(imageNamed: "wheel.png") 
leftWheel.position = CGPoint(x: chassis.position.x - chassis.size.width/2, y: chassis.position.y - wheelOffsetY) //Always set position before physicsBody 
leftWheel.physicsBody = SKPhysicsBody(circleOfRadius: leftWheel.size.width/2) 
leftWheel.physicsBody!.allowsRotation = true; 
vehicle.addChild(leftWheel) 


let rightWheel = SKSpriteNode(imageNamed: "wheel.png") 
rightWheel.position = CGPoint(x: chassis.position.x + chassis.size.width/2, y: chassis.position.y - wheelOffsetY) //Always set position before physicsBody 
rightWheel.physicsBody = SKPhysicsBody(circleOfRadius: leftWheel.size.width/2) 
rightWheel.physicsBody!.allowsRotation = true; 
vehicle.addChild(rightWheel) 


//--------------------- LEFT SUSPENSION ---------------------- // 

let leftShockPost = SKSpriteNode(color: UIColor.blue, size:CGSize(width:7, height: wheelOffsetY)) 

leftShockPost.position = CGPoint(x:chassis.position.x - chassis.size.width/2, y: chassis.position.y - leftShockPost.size.height/2) 

leftShockPost.physicsBody = SKPhysicsBody(rectangleOf: leftShockPost.size) 
vehicle.addChild(leftShockPost) 

let leftSlide = SKPhysicsJointSliding.joint(withBodyA: chassis.physicsBody!, bodyB: leftShockPost.physicsBody!, anchor:CGPoint(x:leftShockPost.position.x, y: leftShockPost.position.y), axis:CGVector(dx: 0.0, dy: 1.0)) 


leftSlide.shouldEnableLimits = true; 
leftSlide.lowerDistanceLimit = 5; 
leftSlide.upperDistanceLimit = wheelOffsetY; 


let leftSpring = SKPhysicsJointSpring.joint(withBodyA: chassis.physicsBody!, bodyB: leftWheel.physicsBody!, anchorA: CGPoint(x:chassis.position.x - chassis.size.width/2, y: chassis.position.y), anchorB: leftWheel.position) 


leftSpring.damping = damping; 
leftSpring.frequency = frequency; 

let lPin = SKPhysicsJointPin.joint(withBodyA: leftShockPost.physicsBody!, bodyB:leftWheel.physicsBody!, anchor:leftWheel.position) 


//--------------------- Right SUSPENSION ---------------------- // 

let rightShockPost = SKSpriteNode(color: UIColor.blue, size:CGSize(width: 7, height: wheelOffsetY)) 

rightShockPost.position = CGPoint(x:chassis.position.x + chassis.size.width/2, y: chassis.position.y - rightShockPost.size.height/2) 

rightShockPost.physicsBody = SKPhysicsBody(rectangleOf: rightShockPost.size) 
vehicle.addChild(rightShockPost) 

let rightSlide = SKPhysicsJointSliding.joint(withBodyA: chassis.physicsBody!, bodyB: rightShockPost.physicsBody!, anchor:CGPoint(x:rightShockPost.position.x, y: rightShockPost.position.y), axis:CGVector(dx: 0.0, dy: 1.0)) 


rightSlide.shouldEnableLimits = true; 
rightSlide.lowerDistanceLimit = 5; 
rightSlide.upperDistanceLimit = wheelOffsetY; 


let rightSpring = SKPhysicsJointSpring.joint(withBodyA: chassis.physicsBody!, bodyB: rightWheel.physicsBody!, anchorA: CGPoint(x: chassis.position.x - chassis.size.width/2, y: chassis.position.y), anchorB: rightWheel.position) 


rightSpring.damping = damping; 
rightSpring.frequency = frequency; 

let rPin = SKPhysicsJointPin.joint(withBodyA: leftShockPost.physicsBody!, bodyB:rightWheel.physicsBody!, anchor:rightWheel.position) 


// Add all joints to the array. 

joints.append(cJoint) 
joints.append(leftSlide) 
joints.append(leftSpring) 
joints.append(rightSlide) 
joints.append(rightSpring) 
joints.append(rPin) 


scene.addChild(vehicle) 
view.presentScene(scene) 
0

आपके स्प्रिंग्स में कोई संपीड़न नहीं है? एक अपराधी के डिफ़ॉल्ट frequency का उपयोग कर रहा है। frequency बढ़ाएं, कहें, 9.0, और कुछ और नहीं बदलें, और मुझे संदेह है कि आप कुछ वांछित संपीड़न देखेंगे।

यह वसंत के "कठोरता" का एक उपाय के रूप में frequency के बारे में सोच के लिए उपयोगी है। उच्च frequency का अर्थ है एक कठोर वसंत। frequency0.0001 बहुत ढीला है! हालांकि, यह तर्क के डिफ़ॉल्ट frequency पर टूट जाता है। frequency == 0.0 पर, वसंत पूरी तरह से कठोर और गैर संपीड़ित है।

संबंधित मुद्दे