Bullet = Object:extend() local pi = math.pi local cos = math.cos local sin = math.sin local atan = math.atan local deg = math.deg function Bullet:new(x, y, p, speed, rotation) self.image = love.graphics.newImage("assets/bullet.png") self.x = x self.y = y self.p = p self.speed = speed self.rotation = rotation self.width = self.image:getWidth() self.height = self.image:getHeight() self.scaleX = 1 self.scaleY = 1 self.originX = self.width / 2 self.originY = self.height / 2 self.collider = World:newCircleCollider(x, y, 10) --self.collider = World:newRectangleCollider(x, y, 10, 10) --self.collider:setType("") self.collider:setFixedRotation(true) self.collider:setPosition(self.x, self.y) self.bounce = 0 -- how many times this bullet has bounced if self.p == 1 then self.collider:setCollisionClass("Bullet1") elseif self.p == 2 then self.collider:setCollisionClass("Bullet2") end end function Bullet:update(dt) -- Calculate the velocity 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 self.bounce = self.bounce + 1 -- Get the collision normal 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) -- 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("################################") -- Debug print to verify reflected velocity --print("Reflected Velocity X:", dx, "Reflected Velocity Y:", dy) 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 -- Set the linear velocity of the collider self.collider:setLinearVelocity(dx, dy) -- Update position self.x = self.collider:getX() self.y = self.collider:getY() end --[[ function Bullet:update(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) end end -- Update position self.x = self.collider:getX() self.y = self.collider:getY() end ]] function Bullet:draw() for _, _ in ipairs(Bullets1) do love.graphics.draw( self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY ) end for _, _ in ipairs(Bullets2) do love.graphics.draw( self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY ) end end