User prompt
Haz que el auto al chocar rebote y pierda velocidad
User prompt
Please fix the bug: 'TypeError: LK.effects.shakeScreen is not a function' in or related to this line: 'LK.effects.shakeScreen(200, 5);' Line Number: 233
User prompt
Agrega física de choque con rebotes
User prompt
Agrega un texto en UI para ver la velocidad actual
User prompt
Suaviza la fricción, no se siente natural y la perdida de velocidad es mucha
User prompt
Agrega fricción al girar
Code edit (1 edits merged)
Please save this source code
User prompt
Agrega drift con momentum sin tweet plugin
User prompt
Agrega momentum ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Mejora la lógica aplicando fuerzas, si el auto acelera y gira el auto se ira un poco de largo manteniendo la posición anterior
User prompt
haz que el auto rote más lento mientras menos velocidad tenga como en la vida real
User prompt
Agrega derrapes
User prompt
Haz que al rotar se pierda muy poco la velocidad
User prompt
Haz que al rotar se pierda un poco la velocidad como cuando en la vida real rotas
User prompt
Haz que el periodo de aceleración dure más para pronuncia mas el tiempo de arranque
User prompt
Pronuncia mas el tiempo de aceleración y aumenta un 50% la velocidad máxima
User prompt
Haz que al soltar el auto mantenga la posición de giro y no se reinicie su rotación
User prompt
Sin tweet plugin
User prompt
Haz que el movimiento del auto sea más realista con aceleración y frenado ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
Cuando se gira todo a la izquierda llega un punto donde el auto gira todo para la derecha para hacer un giro que la para la izquierda es más corto, arregla eso
User prompt
Agrega una formula para el movimiento de car teniendo en cuenta la potencia si es 1 va al máximo de velocidad si va a 0,5 la mitad. Etc
User prompt
Agrega una variable de potencia, valor máximo 1. Si Joystickpoinr está en el centro es 0 si está en los bordes es uno
User prompt
Agrega un pequeño diley de giro para hacerlo natural
User prompt
Haz que la rotación de car player se maneje con joystick
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Create gameplay background - 4/5 of screen height (top portion) var gameplayBackground = game.attachAsset('gameplayBg', { x: 0, y: 0, anchorX: 0, anchorY: 0 }); // Create carPlayer character on top of gameplayBackground var carPlayer = gameplayBackground.attachAsset('CarPlayer', { x: 1024, // Center horizontally y: 1800, // Position in lower portion of gameplay area anchorX: 0.5, anchorY: 0.5 }); // Create UI background - 1/5 of screen height (bottom portion) var uiBackground = game.attachAsset('uiBg', { x: 0, y: 2186, anchorX: 0, anchorY: 0 }); // Create joystickBG centered in UI background var joystickBG = uiBackground.attachAsset('JoystickBG', { x: 1024, // Center horizontally in UI y: 273, // Center vertically in UI (546/2 = 273) anchorX: 0.5, anchorY: 0.5 }); // Create point object that will follow touch position var point = null; // Create JoystickPoinr that will follow point position smoothly var joystickPoinr = game.attachAsset('JoystickPoinr', { x: 1024, y: 2459, anchorX: 0.5, anchorY: 0.5 }); // Variables for smooth movement var targetX = 1024; var targetY = 2459; var smoothSpeed = 0.2; // Variables for smooth rotation var targetRotation = 0; var rotationSpeed = 0.15; // Variables for realistic car physics var currentVelocity = 0; var acceleration = 0.2; var deceleration = 0.95; var maxSpeed = 24; // Momentum variables var momentumX = 0; var momentumY = 0; var momentumDecay = 0.98; var momentumThreshold = 0.1; // Handle touch down - create and show point game.down = function (x, y, obj) { // Create point at touch position point = game.attachAsset('Puntero', { x: x, y: y, anchorX: 0.5, anchorY: 0.5 }); }; // Handle touch move - update point position game.move = function (x, y, obj) { if (point) { point.x = x; point.y = y; // Calculate joystickBG world position var joystickWorldX = joystickBG.x + uiBackground.x; var joystickWorldY = joystickBG.y + uiBackground.y; // Calculate distance from joystick center var deltaX = x - joystickWorldX; var deltaY = y - joystickWorldY; var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); var maxRadius = joystickBG.width / 2; // Limit movement to joystick radius if (distance > maxRadius) { var angle = Math.atan2(deltaY, deltaX); deltaX = Math.cos(angle) * maxRadius; deltaY = Math.sin(angle) * maxRadius; } // Update target position for smooth movement (constrained) targetX = joystickWorldX + deltaX; targetY = joystickWorldY + deltaY; } }; // Handle touch up - remove point game.up = function (x, y, obj) { if (point) { point.destroy(); point = null; // Reset target position to joystick center var joystickWorldX = joystickBG.x + uiBackground.x; var joystickWorldY = joystickBG.y + uiBackground.y; targetX = joystickWorldX; targetY = joystickWorldY; } }; // Update function for smooth movement game.update = function () { // Smoothly move JoystickPoinr towards target position var deltaX = targetX - joystickPoinr.x; var deltaY = targetY - joystickPoinr.y; joystickPoinr.x += deltaX * smoothSpeed; joystickPoinr.y += deltaY * smoothSpeed; // Double-check that joystickPoinr stays within bounds var joystickWorldX = joystickBG.x + uiBackground.x; var joystickWorldY = joystickBG.y + uiBackground.y; var currentDeltaX = joystickPoinr.x - joystickWorldX; var currentDeltaY = joystickPoinr.y - joystickWorldY; var currentDistance = Math.sqrt(currentDeltaX * currentDeltaX + currentDeltaY * currentDeltaY); var maxRadius = joystickBG.width / 2; if (currentDistance > maxRadius) { var angle = Math.atan2(currentDeltaY, currentDeltaX); joystickPoinr.x = joystickWorldX + Math.cos(angle) * maxRadius; joystickPoinr.y = joystickWorldY + Math.sin(angle) * maxRadius; } // Update car rotation based on joystick position var joystickOffsetX = joystickPoinr.x - joystickWorldX; var joystickOffsetY = joystickPoinr.y - joystickWorldY; var joystickDistance = Math.sqrt(joystickOffsetX * joystickOffsetX + joystickOffsetY * joystickOffsetY); // Calculate power based on distance from center (0 to 1) var power = Math.min(joystickDistance / maxRadius, 1); // Only rotate if joystick is moved significantly from center if (joystickDistance > 10) { var joystickAngle = Math.atan2(joystickOffsetX, -joystickOffsetY); targetRotation = joystickAngle; } // Smoothly interpolate car rotation towards target var rotationDelta = targetRotation - carPlayer.rotation; // Handle angle wrapping for shortest rotation path while (rotationDelta > Math.PI) { rotationDelta -= 2 * Math.PI; } while (rotationDelta < -Math.PI) { rotationDelta += 2 * Math.PI; } carPlayer.rotation += rotationDelta * rotationSpeed; // Calculate target velocity based on joystick power var targetVelocity = maxSpeed * power; // Apply acceleration or deceleration without tween if (power > 0.1) { // Accelerating - gradually increase velocity towards target var velocityDiff = targetVelocity - currentVelocity; currentVelocity += velocityDiff * acceleration * 0.1; } else { // Decelerating when joystick is near center currentVelocity *= deceleration; if (Math.abs(currentVelocity) < 0.1) { currentVelocity = 0; } } // Limit velocity to max speed currentVelocity = Math.min(currentVelocity, maxSpeed); // Calculate movement direction based on car rotation and current velocity var moveX = Math.sin(carPlayer.rotation) * currentVelocity; var moveY = -Math.cos(carPlayer.rotation) * currentVelocity; // Add to momentum when actively moving if (power > 0.1) { momentumX += moveX * 0.3; momentumY += moveY * 0.3; } else { // Apply momentum decay when not actively moving momentumX *= momentumDecay; momentumY *= momentumDecay; // Stop very small momentum to prevent infinite drift if (Math.abs(momentumX) < momentumThreshold) momentumX = 0; if (Math.abs(momentumY) < momentumThreshold) momentumY = 0; } // Limit momentum to prevent excessive speeds var momentumMagnitude = Math.sqrt(momentumX * momentumX + momentumY * momentumY); if (momentumMagnitude > maxSpeed * 0.8) { var momentumRatio = maxSpeed * 0.8 / momentumMagnitude; momentumX *= momentumRatio; momentumY *= momentumRatio; } // Update car position with current movement plus momentum carPlayer.x += moveX + momentumX; carPlayer.y += moveY + momentumY; // Keep car within gameplay area bounds var halfCarWidth = 50; // CarPlayer width is 100, so half is 50 var halfCarHeight = 75; // CarPlayer height is 150, so half is 75 if (carPlayer.x < halfCarWidth) { carPlayer.x = halfCarWidth; // Bounce momentum on X collision momentumX = -momentumX * 0.3; tween({ momentum: momentumX }, { momentum: 0 }, { duration: 300, easing: tween.easeOut }); } if (carPlayer.x > 2048 - halfCarWidth) { carPlayer.x = 2048 - halfCarWidth; // Bounce momentum on X collision momentumX = -momentumX * 0.3; tween({ momentum: momentumX }, { momentum: 0 }, { duration: 300, easing: tween.easeOut }); } if (carPlayer.y < halfCarHeight) { carPlayer.y = halfCarHeight; // Bounce momentum on Y collision momentumY = -momentumY * 0.3; tween({ momentum: momentumY }, { momentum: 0 }, { duration: 300, easing: tween.easeOut }); } if (carPlayer.y > 2186 - halfCarHeight) { carPlayer.y = 2186 - halfCarHeight; // Bounce momentum on Y collision momentumY = -momentumY * 0.3; tween({ momentum: momentumY }, { momentum: 0 }, { duration: 300, easing: tween.easeOut }); } };
===================================================================
--- original.js
+++ change.js
@@ -1,5 +1,10 @@
/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
@@ -60,13 +65,13 @@
var currentVelocity = 0;
var acceleration = 0.2;
var deceleration = 0.95;
var maxSpeed = 24;
-// Drift physics variables
-var velocityX = 0;
-var velocityY = 0;
-var driftFactor = 0.85; // How much the car maintains sideways momentum (lower = more drift)
-var gripFactor = 0.15; // How quickly the car aligns with its facing direction
+// Momentum variables
+var momentumX = 0;
+var momentumY = 0;
+var momentumDecay = 0.98;
+var momentumThreshold = 0.1;
// Handle touch down - create and show point
game.down = function (x, y, obj) {
// Create point at touch position
point = game.attachAsset('Puntero', {
@@ -150,11 +155,9 @@
}
while (rotationDelta < -Math.PI) {
rotationDelta += 2 * Math.PI;
}
- // Calculate rotation speed based on current velocity (slower rotation at lower speeds)
- var velocityBasedRotationSpeed = rotationSpeed * Math.max(0.3, currentVelocity / maxSpeed);
- carPlayer.rotation += rotationDelta * velocityBasedRotationSpeed;
+ carPlayer.rotation += rotationDelta * rotationSpeed;
// Calculate target velocity based on joystick power
var targetVelocity = maxSpeed * power;
// Apply acceleration or deceleration without tween
if (power > 0.1) {
@@ -169,52 +172,85 @@
}
}
// Limit velocity to max speed
currentVelocity = Math.min(currentVelocity, maxSpeed);
- // Calculate intended movement direction based on car rotation and current velocity
- var intendedMoveX = Math.sin(carPlayer.rotation) * currentVelocity;
- var intendedMoveY = -Math.cos(carPlayer.rotation) * currentVelocity;
- // Calculate momentum from previous frame (inertia effect)
- var momentumX = velocityX * 0.92; // Maintain 92% of previous horizontal momentum
- var momentumY = velocityY * 0.92; // Maintain 92% of previous vertical momentum
- // Apply force-based physics - combine momentum with intended direction
- // The car will slide in its previous direction while gradually aligning to new direction
- velocityX = momentumX + intendedMoveX * gripFactor;
- velocityY = momentumY + intendedMoveY * gripFactor;
- // Add additional drift when turning at high speeds
- if (currentVelocity > maxSpeed * 0.5) {
- var turnForce = Math.abs(rotationDelta) * currentVelocity * 0.3;
- // Apply lateral force perpendicular to car direction when turning
- var perpX = -Math.cos(carPlayer.rotation) * turnForce;
- var perpY = -Math.sin(carPlayer.rotation) * turnForce;
- // Determine turn direction and apply lateral force
- if (rotationDelta > 0) {
- velocityX += perpX;
- velocityY += perpY;
- } else {
- velocityX -= perpX;
- velocityY -= perpY;
- }
+ // Calculate movement direction based on car rotation and current velocity
+ var moveX = Math.sin(carPlayer.rotation) * currentVelocity;
+ var moveY = -Math.cos(carPlayer.rotation) * currentVelocity;
+ // Add to momentum when actively moving
+ if (power > 0.1) {
+ momentumX += moveX * 0.3;
+ momentumY += moveY * 0.3;
+ } else {
+ // Apply momentum decay when not actively moving
+ momentumX *= momentumDecay;
+ momentumY *= momentumDecay;
+ // Stop very small momentum to prevent infinite drift
+ if (Math.abs(momentumX) < momentumThreshold) momentumX = 0;
+ if (Math.abs(momentumY) < momentumThreshold) momentumY = 0;
}
- // Update car position using drift-affected velocity
- carPlayer.x += velocityX;
- carPlayer.y += velocityY;
+ // Limit momentum to prevent excessive speeds
+ var momentumMagnitude = Math.sqrt(momentumX * momentumX + momentumY * momentumY);
+ if (momentumMagnitude > maxSpeed * 0.8) {
+ var momentumRatio = maxSpeed * 0.8 / momentumMagnitude;
+ momentumX *= momentumRatio;
+ momentumY *= momentumRatio;
+ }
+ // Update car position with current movement plus momentum
+ carPlayer.x += moveX + momentumX;
+ carPlayer.y += moveY + momentumY;
// Keep car within gameplay area bounds
var halfCarWidth = 50; // CarPlayer width is 100, so half is 50
var halfCarHeight = 75; // CarPlayer height is 150, so half is 75
if (carPlayer.x < halfCarWidth) {
carPlayer.x = halfCarWidth;
- velocityX = Math.abs(velocityX) * 0.3; // Bounce back with reduced velocity
+ // Bounce momentum on X collision
+ momentumX = -momentumX * 0.3;
+ tween({
+ momentum: momentumX
+ }, {
+ momentum: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut
+ });
}
if (carPlayer.x > 2048 - halfCarWidth) {
carPlayer.x = 2048 - halfCarWidth;
- velocityX = -Math.abs(velocityX) * 0.3; // Bounce back with reduced velocity
+ // Bounce momentum on X collision
+ momentumX = -momentumX * 0.3;
+ tween({
+ momentum: momentumX
+ }, {
+ momentum: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut
+ });
}
if (carPlayer.y < halfCarHeight) {
carPlayer.y = halfCarHeight;
- velocityY = Math.abs(velocityY) * 0.3; // Bounce back with reduced velocity
+ // Bounce momentum on Y collision
+ momentumY = -momentumY * 0.3;
+ tween({
+ momentum: momentumY
+ }, {
+ momentum: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut
+ });
}
if (carPlayer.y > 2186 - halfCarHeight) {
carPlayer.y = 2186 - halfCarHeight;
- velocityY = -Math.abs(velocityY) * 0.3; // Bounce back with reduced velocity
+ // Bounce momentum on Y collision
+ momentumY = -momentumY * 0.3;
+ tween({
+ momentum: momentumY
+ }, {
+ momentum: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut
+ });
}
};
\ No newline at end of file