Code edit (2 edits merged)
Please save this source code
User prompt
in calculateVelocity divide boot velocityX and velocityY by 5
User prompt
If bouncing happens, after updating self.velocityX and self.velocityY, separately *= both by bounceFactor
User prompt
If bouncing happens, after updating self.velocityX and self.velocityY, separately *= both by bounceFactor
User prompt
In bounce if the ball bounces, add two lines, one multiplying velocityX with .7 and one one for Y as well
User prompt
After updating self.velocityX and Y multiply both with bounceFactor
Code edit (1 edits merged)
Please save this source code
User prompt
Velocity should be lost when bouncing on boot
User prompt
set realWidth to .85 of asset width
User prompt
The bounce code seems to make the ball go faster and faster. Update the bounce code to use circle to circle intersection between ball and boot factoring in ball speed and boot speed to calculate the new speed of the ball. similar to if two billiard balls where to hit each other
User prompt
when calculating dot product, subtract ball velocity rather than adding it
User prompt
also factor in ball speed when calculating dot product
Code edit (1 edits merged)
Please save this source code
User prompt
also factor in ball speed when calculating dotProduct
Code edit (2 edits merged)
Please save this source code
User prompt
don't divide velocity by 5
Code edit (2 edits merged)
Please save this source code
User prompt
Instead of using boot.width in game use realWidth
User prompt
Create a realWidth property on boot and set it to 70% of the width of the bootGraphics
User prompt
Assume the boot is 30% smaller than it is
Code edit (1 edits merged)
Please save this source code
User prompt
Decrease velocity from bounce by 5x
User prompt
Add 20 degrees to the boot rotation always
User prompt
Make sure velocityX exists on ball
Code edit (4 edits merged)
Please save this source code
var Boot = Container.expand(function () { var self = Container.call(this); var bootGraphics = self.createAsset('boot', 'Boot', .5, .5); self.realWidth = bootGraphics.width * 0.85; self.velocityX = 0; self.velocityY = 0; self.previousX = 0; self.previousY = 0; self.calculateVelocity = function () { self.velocityX = (self.x - self.previousX) / 5; self.velocityY = (self.y - self.previousY) / 5; self.previousX = self.x; self.previousY = self.y; var targetRotation = -Math.atan2(self.velocityY, 0) / 2 + Math.PI / 9; self.rotation += (targetRotation - self.rotation) * 0.1; }; }); var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.createAsset('ball', 'Soccer Ball', .5, .5); self.velocityX = 0; self.velocityY = 0; self.gravity = 0.5; self.bounceFactor = 0.7; self.move = function () { self.x += self.velocityX; self.y += self.velocityY; self.velocityY += self.gravity; if (self.x - self.width / 2 < 0) { self.velocityX *= -0.7; self.x = self.width / 2; } if (self.x + self.width / 2 > 2048) { self.velocityX *= -0.7; self.x = 2048 - self.width / 2; } }; self.bounce = function (boot) { var dx = self.x - boot.x; var dy = self.y - boot.y; var distance = Math.sqrt(dx * dx + dy * dy); var normalX = dx / distance; var normalY = dy / distance; var dotProduct = -normalX * (boot.velocityX - self.velocityX) - normalY * (boot.velocityY - self.velocityY); if (distance < boot.realWidth / 2 + self.width / 2) { var overlap = boot.realWidth / 2 + self.width / 2 - distance + 1; self.x += overlap * directionX; self.y += overlap * directionY; var directionX = dx / distance; var directionY = dy / distance; var relativeVelocityX = self.velocityX - boot.velocityX; var relativeVelocityY = self.velocityY - boot.velocityY; var speed = relativeVelocityX * directionX + relativeVelocityY * directionY; var velocityAlongX = directionX * speed; var velocityAlongY = directionY * speed; self.velocityX -= 2 * velocityAlongX; self.velocityY -= 2 * velocityAlongY; self.velocityX += boot.velocityX; self.velocityY += boot.velocityY; self.velocityX *= self.bounceFactor; self.velocityY *= self.bounceFactor; } }; }); var Game = Container.expand(function () { var self = Container.call(this); stage.on('move', function (obj) { var pos = obj.event.getLocalPosition(self); boot.x = pos.x; boot.y = pos.y; }); var ball = self.addChild(new Ball()); ball.x = 2048 / 2; ball.y = 2732 / 4; var boot = self.addChild(new Boot()); boot.visible = true; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var isGameOver = false; var isGameStarted = false; stage.on('down', function (obj) { var pos = obj.event.getLocalPosition(self); boot.x = pos.x; boot.y = pos.y; boot.visible = true; }); LK.on('tick', function () { boot.calculateVelocity(); if (!isGameStarted) { var dx = boot.x - ball.x; var dy = boot.y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < boot.realWidth / 2 + ball.width / 2) { ball.bounce(boot); isGameStarted = true; } } else { ball.move(); var dx = boot.x - ball.x; var dy = boot.y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < boot.realWidth / 2 + ball.width / 2) { ball.bounce(boot); } } if (ball.y > 2732 - ball.height / 2) { isGameOver = true; } if (isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }); });
===================================================================
--- original.js
+++ change.js
@@ -42,8 +42,11 @@
var normalX = dx / distance;
var normalY = dy / distance;
var dotProduct = -normalX * (boot.velocityX - self.velocityX) - normalY * (boot.velocityY - self.velocityY);
if (distance < boot.realWidth / 2 + self.width / 2) {
+ var overlap = boot.realWidth / 2 + self.width / 2 - distance + 1;
+ self.x += overlap * directionX;
+ self.y += overlap * directionY;
var directionX = dx / distance;
var directionY = dy / distance;
var relativeVelocityX = self.velocityX - boot.velocityX;
var relativeVelocityY = self.velocityY - boot.velocityY;
@@ -55,11 +58,8 @@
self.velocityX += boot.velocityX;
self.velocityY += boot.velocityY;
self.velocityX *= self.bounceFactor;
self.velocityY *= self.bounceFactor;
- var overlap = boot.realWidth / 2 + self.width / 2 - distance + 1;
- self.x += overlap * directionX;
- self.y += overlap * directionY;
}
};
});
var Game = Container.expand(function () {
Soccer ball. Single Cartoon Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Round soccer boot, cartoon style Single Cartoon Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Cartoon football stadium Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.