|
|
@ -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 |
|
|
|
if self.collider:enter("Wall") then |
|
|
|
local dy = math.sin(self.rotation) * self.speed * dt |
|
|
|
--calculate normal of the wall |
|
|
|
|
|
|
|
--calculate the angle of reflection |
|
|
|
|
|
|
|
local angle = (math.atan2(dx, dy)) |
|
|
|
|
|
|
|
--get normal of the wall without using getNormal() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----TODO: fix this |
|
|
|
-- Check for collisions |
|
|
|
|
|
|
|
if self.collider:enter("Wall") then |
|
|
|
|
|
|
|
self.bounce = self.bounce + 1 |
|
|
|
|
|
|
|
|
|
|
|
--set the new angle |
|
|
|
-- Get the collision normal |
|
|
|
self.rotation = angle |
|
|
|
local collision_data = self.collider:getEnterCollisionData("Wall") |
|
|
|
self.collider:setAngle(self.rotation) |
|
|
|
local contact = collision_data.contact |
|
|
|
end |
|
|
|
local normal_x, normal_y = contact:getNormal() |
|
|
|
dx = cos(self.rotation) * self.speed * dt |
|
|
|
|
|
|
|
dy = sin(self.rotation) * self.speed * dt |
|
|
|
|
|
|
|
self.collider:setLinearVelocity(dx, dy) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.p == 2 then |
|
|
|
-- Debug print to verify normal values |
|
|
|
local dx = cos(self.rotation) * self.speed * dt |
|
|
|
--print("---------------------------------") |
|
|
|
local dy = sin(self.rotation) * self.speed * dt |
|
|
|
--print("Collision detected!") |
|
|
|
self.collider:setLinearVelocity(dx, dy) |
|
|
|
--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 |
|
|
|
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.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 |
|
|
|