Bullet traveling along straight line

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Bullet traveling along straight line

Post by nevon »

I'm having some serious problem implementing something that I thought would be a complete piece of cake, and it's driving me nuts.

We're basically talking about something as simple as plotting a line from one point to another, and then have another point travel along that line. Just like a bullet from a gun (or in this case, an orange dot from the nose of a spaceship).

A shot is created like this:

Code: Select all

table.insert(projectiles.playershots, {
    start={ -- The starting position of the shot
        x=player.x,
        y=player.y
    }, 
    target = { -- Where we want the shot to go (x and y are mouse coordinates)
        x=x,
        y=y
    },
    position={ -- The current position of the shot
        x=player.x,
        y=player.y
    },
    direction = math.atan2((y-player.y+player.images.normal.height),(x-player.x+player.images.normal.width/2)), --The angle of the shot
    v = 100 -- the speed
})
In the update function I have this to update the position of the shots:

Code: Select all

local i = 1
while i <= table.getn(projectiles.playershots) do
    projectiles.playershots[i].position.y = projectiles.playershots[i].position.y - math.abs( math.cos( projectiles.playershots[i].direction + math.rad( -90 ) ) * projectiles.playershots[i].v * dt )

    projectiles.playershots[i].position.x = projectiles.playershots[i].position.x - math.sin( projectiles.playershots[i].direction + math.rad( -90 ) ) * projectiles.playershots[i].v * dt

    i = i+1
end
To get an idea of what it looks like. Here's a screenshot.

Image

It might not be obvious from the screenshot, but the angle of the shots is a bit off, and I can't figure out why. I'm doing something wrong, but for the life of me I can't figure out what. I've checked whether I'm just drawing the images wrong, or something dumb like that, but even when I replace them with just points it's still off. The offset seems to vary depending on the distance between the two point, so something must be wrong with what I'm doing.

I've never studied trigonometry, and man do I regret that now! It's a small miracle that I've even managed to get this far, but now I really need some help.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Bullet traveling along straight line

Post by bmelts »

your update function would look a lot better like this:

Code: Select all

for i,v in ipairs(projectiles.playershots) do
    v.position.y = v.position.y - math.abs(math.cos(v.direction + math.rad(-90) ) * v.v * dt )
    v.position.x = v.position.x - math.sin(v.direction + math.rad(-90) ) * v.v * dt )
end
As for the math: I don't quite understand the offsets for the player in the direction function. Assuming the origin is the upper left corner, like the image used to represent the player, you shouldn't need the extra offset in the y calculation. (The x one is still necessary if you want the shot to come from the middle of the ship.)
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: Bullet traveling along straight line

Post by mikembley »

Two relatively basic functions i use, which are similar to anjos code

Code: Select all

--Returns the x vector from {len} and {dir}
function lengthdir_x( len, dir )
	local dir = math.rad( dir )
	return math.cos( dir ) * len
end

--Returns the y vector from {len} and {dir}
function lengthdir_y( len, dir )
	local dir = math.rad( dir )
	return -math.sin( dir ) * len
end
To use you would just add the vector to the objects current x,y like so

Code: Select all

object = { x=100, y=100 }
object.x = object.x + lengthdir_x(100, 45) -- Will move the object by 100, in the direction of 45 degrees
object.y = object.y + lengthdir_y(100, 45) -- Ditto
Hope it helps, if you need more understanding or clarification ill help :)
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Bullet traveling along straight line

Post by nevon »

Thank you. Your approaches seem far "cleaner". Sadly, neither of them solved my problem. However, I was able to figure out what was causing the misaligned trajectories. It's almost embarrassing to admit, but when I calculated the direction for each projectile, I added an offset, because I wanted the projectile to go from the nose of the ship. Originally that was needed, because player.position.x and player.position.y where calculated from the top left of the image, but I had since changed that so that the coordinates pointed to the nose of the ship; thus the offset was no longer needed, and that's what caused my calculations to be off.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest