Set the physical body at a constant speed

Constant speed ball movement

I tried to make a box of balls where the balls move at a constant speed. They should not slow down when they collide with each other. I think I set all the properties correctly, but it didn’t work, and after 30 seconds all the balls stopped to move.

The box is installed as follows:

    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody.dynamic = false
    self.physicsBody.restitution = 1
    self.physicsBody.friction = 0

Balls are set like this:

enter image description here

Is this a mistake in the physics engine or am I missing something?

+4
source share
2 answers

, , , SKScene. ... , .;)

, / ():

CGPoint velocity = somePhysicsBody.velocity;
velocity = normalized(velocity);
velocity = multiply(velocity, desiredSpeed);
somePhysicsBody.velocity = velocity;
+4

Steffen (LearnCocos2D) , , Ray Wenderlich tutorial.

static inline CGPoint rwAdd(CGPoint a, CGPoint b) {
    return CGPointMake(a.x + b.x, a.y + b.y);
}

static inline CGPoint rwSub(CGPoint a, CGPoint b) {
    return CGPointMake(a.x - b.x, a.y - b.y);
}

static inline CGPoint rwMult(CGPoint a, float b) {
    return CGPointMake(a.x * b, a.y * b);
}

static inline float rwLength(CGPoint a) {
    return sqrtf(a.x * a.x + a.y * a.y);
}

// Makes a vector have a length of 1
static inline CGPoint rwNormalize(CGPoint a) {
    float length = rwLength(a);
    return CGPointMake(a.x / length, a.y / length);
}

CGPoint , , CGVector.

, . .

+1

Source: https://habr.com/ru/post/1544864/


All Articles