better bouncing finally

main
Simon Kellet 4 months ago
parent f5770cd4e0
commit 49a57a6e5d
  1. 79
      bullet.lua

@ -1,7 +1,9 @@
Bullet = Object:extend() Bullet = Object:extend()
cos = math.cos local pi = math.pi
sin = math.sin --optimisation local cos = math.cos
pi = math.pi local sin = math.sin
local atan = math.atan
local deg = math.deg
function Bullet:new(x, y, p, speed, rotation) function Bullet:new(x, y, p, speed, rotation)
self.image = love.graphics.newImage("assets/bullet.png") self.image = love.graphics.newImage("assets/bullet.png")
@ -20,9 +22,13 @@ function Bullet:new(x, y, p, speed, rotation)
self.originY = self.height / 2 self.originY = self.height / 2
self.collider = World:newCircleCollider(x, y, 10) self.collider = World:newCircleCollider(x, y, 10)
--self.collider = World:newRectangleCollider(x, y, 10, 10)
--self.collider:setType("")
self.collider:setFixedRotation(true) self.collider:setFixedRotation(true)
self.collider:setPosition(self.x, self.y) self.collider:setPosition(self.x, self.y)
self.bounce = 0 -- how many times this bullet has bounced
if self.p == 1 then if self.p == 1 then
self.collider:setCollisionClass("Bullet1") self.collider:setCollisionClass("Bullet1")
elseif self.p == 2 then elseif self.p == 2 then
@ -31,34 +37,69 @@ function Bullet:new(x, y, p, speed, rotation)
end end
function Bullet:update(dt) function Bullet:update(dt)
--If a bullet hits a wall, make it bounce off the wall and change its rotation -- Calculate the velocity
if self.p == 1 then local dx = math.cos(self.rotation) * self.speed * dt
local dy = math.sin(self.rotation) * self.speed * dt
-- Check for collisions
if self.collider:enter("Wall") then if self.collider:enter("Wall") then
--calculate normal of the wall self.bounce = self.bounce + 1
--calculate the angle of reflection
local angle = (math.atan2(dx, dy)) -- Get the collision normal
--get normal of the wall without using getNormal() local collision_data = self.collider:getEnterCollisionData("Wall")
local contact = collision_data.contact
local normal_x, normal_y = contact:getNormal()
-- Debug print to verify normal values
--print("---------------------------------")
--print("Collision detected!")
--print("Normal X:", normal_x, "Normal Y:", normal_y)
--print("Initial Velocity X:", dx, "Initial Velocity Y:", dy)
--print("Initial Angle: ", self.rotation)
----TODO: fix this -- Reflect the velocity using the normal
local dot_product = dx * normal_x + dy * normal_y
dx = dx - 2 * dot_product * normal_x
dy = dy - 2 * dot_product * normal_y
--print("################################")
--set the new angle -- Debug print to verify reflected velocity
self.rotation = angle --print("Reflected Velocity X:", dx, "Reflected Velocity Y:", dy)
self.collider:setAngle(self.rotation) self.rotation = deg(atan(dy, dx))
-- Update the rotation based on the new velocity direction
--self.rotation = deg(atan(dy, dx))
--print("Reflected Angle (radians):", self.rotation)
--print("Reflected Angle (degrees):", self.rotation)
--print("---------------------------------")
end end
dx = cos(self.rotation) * self.speed * dt
dy = sin(self.rotation) * self.speed * dt -- Set the linear velocity of the collider
self.collider:setLinearVelocity(dx, dy) self.collider:setLinearVelocity(dx, dy)
-- Update position
self.x = self.collider:getX()
self.y = self.collider:getY()
end end
if self.p == 2 then --[[
local dx = cos(self.rotation) * self.speed * dt function Bullet:update(dt)
local dy = sin(self.rotation) * self.speed * dt local normal_x, normal_y = 0, 0
local dx, dy = 0, 0
if self.p == 1 or self.p == 2 then
if self.collider:enter("Wall") then
self.collider:setLinearVelocity(dx, dy)
else
dx = cos(self.rotation) * self.speed * dt
dy = sin(self.rotation) * self.speed * dt
self.collider:setLinearVelocity(dx, dy) self.collider:setLinearVelocity(dx, dy)
end end
end
-- Update position
self.x = self.collider:getX() self.x = self.collider:getX()
self.y = self.collider:getY() self.y = self.collider:getY()
end end
]]
function Bullet:draw() function Bullet:draw()
for _, _ in ipairs(Bullets1) do for _, _ in ipairs(Bullets1) do

Loading…
Cancel
Save