|
|
|
@ -1,7 +1,9 @@ |
|
|
|
|
Bullet = Object:extend() |
|
|
|
|
cos = math.cos |
|
|
|
|
sin = math.sin --optimisation |
|
|
|
|
pi = math.pi |
|
|
|
|
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") |
|
|
|
@ -20,9 +22,13 @@ function Bullet:new(x, y, p, speed, rotation) |
|
|
|
|
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 |
|
|
|
@ -31,34 +37,69 @@ function Bullet:new(x, y, p, speed, rotation) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
function Bullet:update(dt) |
|
|
|
|
--If a bullet hits a wall, make it bounce off the wall and change its rotation |
|
|
|
|
if self.p == 1 then |
|
|
|
|
if self.collider:enter("Wall") then |
|
|
|
|
--calculate normal of the wall |
|
|
|
|
--calculate the angle of reflection |
|
|
|
|
local angle = (math.atan2(dx, dy)) |
|
|
|
|
--get normal of the wall without using getNormal() |
|
|
|
|
-- Calculate the velocity |
|
|
|
|
local dx = math.cos(self.rotation) * self.speed * dt |
|
|
|
|
local dy = math.sin(self.rotation) * self.speed * dt |
|
|
|
|
|
|
|
|
|
----TODO: fix this |
|
|
|
|
-- Check for collisions |
|
|
|
|
if self.collider:enter("Wall") then |
|
|
|
|
self.bounce = self.bounce + 1 |
|
|
|
|
|
|
|
|
|
--set the new angle |
|
|
|
|
self.rotation = angle |
|
|
|
|
self.collider:setAngle(self.rotation) |
|
|
|
|
end |
|
|
|
|
dx = cos(self.rotation) * self.speed * dt |
|
|
|
|
dy = sin(self.rotation) * self.speed * dt |
|
|
|
|
self.collider:setLinearVelocity(dx, dy) |
|
|
|
|
end |
|
|
|
|
-- Get the collision normal |
|
|
|
|
local collision_data = self.collider:getEnterCollisionData("Wall") |
|
|
|
|
local contact = collision_data.contact |
|
|
|
|
local normal_x, normal_y = contact:getNormal() |
|
|
|
|
|
|
|
|
|
if self.p == 2 then |
|
|
|
|
local dx = cos(self.rotation) * self.speed * dt |
|
|
|
|
local dy = sin(self.rotation) * self.speed * dt |
|
|
|
|
self.collider:setLinearVelocity(dx, dy) |
|
|
|
|
-- 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 |
|
|
|
|