Glitches with the automatic machine gun

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
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Glitches with the automatic machine gun

Post by HedgeHog builder »

So this is the problem:
I am a white square following the mouse.
The machinegun the AI is firing however is not pointing in the correct direction.
It's origin is not even near the green AI
...
What I would wish:
The machine gun actually aims towards the player.
What might be wrong:
Check my MissileFire function first, I think there is something wrong with the mathematics there.
Attachments
main.lua
(1.85 KiB) Downloaded 87 times
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Glitches with the automatic machine gun

Post by sphyrth »

So let's just get this one in:
asdfasdfasdf.png
asdfasdfasdf.png (10.26 KiB) Viewed 2956 times
The white square is you.
The green square in the middle is an AI (enemy?)
And that other green thing is the machine gun.

Now, how should your game work again?
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Glitches with the automatic machine gun

Post by sphyrth »

Okay, I think I got a good handle of what's going on. Let's start with your starting coordinates:

Code: Select all

function EnemyMissile(x1,x2,y1,y2)
	local StartX = x1/2
	local StartY = y1/2
	...
On this part, it seems that you want the Missiles start coming out from the CENTER of the enemy. But you first have to define the enemy's width and height before you can do that. And, also you're referencing the player rather than the enemy.

So to simplify things, I did this:

Code: Select all

function EnemyMissile(x1,x2,y1,y2)
	local StartX = x2
	local StartY = y2
	...
It's a step, but we got the enemy shooting missiles. Now we have to solve for the correct direction.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Glitches with the automatic machine gun

Post by sphyrth »

I got it. This is your original code

Code: Select all

function EnemyMissile(x1,x2,y1,y2)
   ...
   local angle = math.atan2((x1 - x2), (y1 - y2))
Just switch the x's an y's in the math.atan2():

Code: Select all

function EnemyMissile(x1,x2,y1,y2)
   ...
   local angle = math.atan2((y1 - y2), (x1 - x2))
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Glitches with the automatic machine gun

Post by HedgeHog builder »

Thanks sphyrth!
Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests