P5.js Asteroid Game Question

How to move the Bullet into the side of the Ship? Not the angle but on where is the bullet is coming from. I want to create guns on each side because I want to be more creative. But the problem is that everytime the ships turn, the bullet point is not moving. Its just changing its angle. Please help me out. Thank you :smiley:Capture

Can you post your code here ? Without it we don’t know how it is structured and thus we can’t really help.

I presume that you have a “Bullet” class and a “SpaceShip” one.

In that case, from you “Bullet” class you would need to get the current position and rotation of the spaceship and then use trigonometric equations to position the two symmetric bullets.

This is the Bullet , its called Laser

232

Im calling out the Lasers if I enabled the spaceHeld.

asdasd

This is how I turn my Space Ship

So when you create your laser, instead of writing

this.pos = createVector(spos.x, spos.y);

you should write

this.pos1 = createVector(spos.x + r * cos(angle - theta), spos.y + r * sin(angle - theta)); this.pos2 = createVector(spos.x + r * cos(angle + theta), spos.y + r * sin(angle + theta));

I’ll make a pic to explain a bit more.

1 Like

Please. I need more explanation :disappointed_relieved: … It seems here that I need to learn more about this algorithm.

Example

You need to find the coordinate of the bullet in the spaceship coordinate thanks to the angle of the spaceship, r and theta.

Then you just need to add the coordinate of the origin of your spaceship. This is the formulas I gave you in my previous answer.

1 Like

It says that theta is not defined. I searched on google and they say that theta is 90-22 degree angle.

“Theta” and “r” are yours to define. Those are not absolute values. I could have name those variables alpha or beta or whatever instead of theta. It is just that theta is often use to define an angle value.

1 Like

dddda!
It worked! Thank you so much @jb4x for helping me out. I’ll add you on the credit list :smiley:

Glad it worked out !

Hope to see your game in the gallery section =)

1 Like

Soon :smile: I hope so. :laughing:

BTW. Is there anyway to reduce lag ? Or a technic ?

If your game is getting slow it’s because you have to many computation to do at each frame.

In this case you need to optimize your code :

  • be sure to delete all the objects that you don’t need anymore (bullets out of screen for examples)
  • Avoid to recalculate variables at every frame if you can “bake” them before
  • instead of using cpu you can use the gpu. But there it becomes way out of my knowledge, I’m just using processing (or coding in general) on my free time for personal project.

Is there any program or any code that I can add to remove the lag?

Im already doing those things. Im using splice in removing the bullets out of screen.

Im also making my if statements smaller and compact to reduce lag.

This old sketch demos how to shoot bullets w/ angle: :gun:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp