2016-08-15 65 views
-1

我想爲我的賽車模擬做一個圖形輸出,所以我找到了paper.js。paper.js Racing Track

有了這段代碼,我可以繪製每輛車的特定路線。但我不知道如何改變每一輪的速度:speed_car1,speed_car2,speed_car3。所以我想改變每一輛汽車的速度。

這裏是我的example

var car1 = new Shape.Rectangle({ 
    from: [0, 0], 
    to: [10, 5], 
    fillColor: 'red' 
}); 

var car2 = new Shape.Rectangle({ 
    from: [0, 0], 
    to: [10, 5], 
    fillColor: 'blue' 
}); 

var car3 = new Shape.Rectangle({ 
    from: [0, 0], 
    to: [10, 5], 
    fillColor: 'yellow' 
}); 

var speed_car1 = 1.0; 
var speed_car2 = 1.2; 
var speed_car3 = 1.5; 

var diagonal = new Point(view.size).length; 

var path = new Path(); 
path.strokeColor = 'black'; 
path.add(new Point(60, 130)); 
path.add(new Point(100, 100)); 
path.add(new Point(200, 50)); 
path.add(new Point(280, 60)); 
path.add(new Point(340, 70)); 
path.add(new Point(500, 250)); 
path.add(new Point(470, 320)); 
path.add(new Point(100, 320)); 
path.add(new Point(60, 130)); 
path.smooth(); 

car1.onFrame = function(event) { 
    var offset = event.count * speed_car1; 
    var loc = path.getLocationAt(offset % path.length); 
    if (loc) { 
     this.position = loc.point; 
     this.rotation = loc.tangent.angle; 
    } 
} 
car2.onFrame = function(event) { 
    var offset = event.count * speed_car2; 
    var loc = path.getLocationAt(offset % path.length); 
    if (loc) { 
     this.position = loc.point; 
     this.rotation = loc.tangent.angle; 
    } 
} 
car3.onFrame = function(event) { 
    var offset = event.count * speed_car3; 
    var loc = path.getLocationAt(offset % path.length); 
    if (loc) { 
     this.position = loc.point; 
     this.rotation = loc.tangent.angle; 
    } 
} 

回答

0

你可以使用類似的計算,你使用來確定位置的一個:

var car1 = new Shape.Rectangle({ 
 
    from: [0, 0], 
 
    to: [10, 5], 
 
    fillColor: 'red' 
 
}); 
 

 
var car2 = new Shape.Rectangle({ 
 
    from: [0, 0], 
 
    to: [10, 5], 
 
    fillColor: 'blue' 
 
}); 
 
    
 
var car3 = new Shape.Rectangle({ 
 
    from: [0, 0], 
 
    to: [10, 5], 
 
    fillColor: 'yellow' 
 
}); 
 

 
var speed_car1 = 1.0; 
 
var speed_car2 = 1.2; 
 
var speed_car3 = 1.5; 
 

 
var diagonal = new Point(view.size).length; 
 

 
var path = new Path(); 
 
path.strokeColor = 'black'; 
 
path.add(new Point(60, 130)); 
 
path.add(new Point(100, 100)); 
 
path.add(new Point(200, 50)); 
 
path.add(new Point(280, 60)); 
 
path.add(new Point(340, 70)); 
 
path.add(new Point(500, 250)); 
 
path.add(new Point(470, 320)); 
 
path.add(new Point(100, 320)); 
 
path.add(new Point(60, 130)); 
 
path.smooth(); 
 

 
car1.onFrame = function(event) { 
 
    var offset = event.count * speed_car1; 
 
    var loc = path.getLocationAt(offset % path.length); 
 
    if (loc) { 
 
     this.position = loc.point; 
 
     this.rotation = loc.tangent.angle; 
 
    } 
 
} 
 
car2.onFrame = function(event) { 
 
    var offset = event.count * speed_car2; 
 
    var loc = path.getLocationAt(offset % path.length); 
 
    if (loc) { 
 
     this.position = loc.point; 
 
     this.rotation = loc.tangent.angle; 
 
    } 
 
} 
 
car3.onFrame = function(event) { 
 
    var offset = event.count * speed_car3; 
 
    var loc = path.getLocationAt(offset % path.length); 
 
    if (loc) { 
 
     this.position = loc.point; 
 
     this.rotation = loc.tangent.angle; 
 
    } 
 
    // compare offset with some epsilon 
 
    if ((offset % path.length) < 1) { 
 
     // adjust speed 
 
     speed_car3 = speed_car3 *2 }; 
 
}

+0

感謝了很多,但我有一個問題。如果我使用數組有一個錯誤。有時候汽車稍微後退一點。在這個例子中,賽車在第3圈略微回落:goo.gl/Q0ED6Y(短鏈接,因爲我不能在評論中寫完整鏈接:() – Lehtis

+0

好吧,我發現我的問題;)(event.count是問題。我爲此做了一個新的變量,每個onFrame我加1,如果汽車通過了開始,那麼我將它設置爲0.所以問題就沒有了,非常感謝您的幫助;) – Lehtis