I want the projectiles to spawn at the end of the gun-barrel while the arm is waving and flailing about. Each gun has a different muzzle-offset. How would I go about figuring out the new muzzle-offset for the projectiles after I rotate the arms? Can I somehow rotate a point using the same transform-matrix as I do for the arm (which is calculated every update() so far; I will eventually change that, so it doesn't run unnecessarily)?

I've been pulling hair out of my already balding head for days pondering over this. I mean, I've tried circle-math and stuff, but I'm just too confused about which points are important in this endeavour, and which steps to take. I finally got the arms to rotate properly, though.

Note: This is using nothing but Java2D. I know... For my next project I'll use LWJGL

// set init position and anglep.setInitX(originX-(a.getImage().getWidth()/2));p.setInitY(originY-(a.getImage().getHeight()/2));p.setPosX(originX-(a.getImage().getWidth()/2));p.setPosY(originY-(a.getImage().getHeight()/2));p.setAngle(angle);p.setTimeLived(0);

I wouldn't only rotate the sprite. (That could lead to Collision problems...)

I'd do it with trigonometry. Heard from sinus and cosinus?

Use them here

You could set other starting positions for the bullet:

1 2

doublebulletX = Math.cos(Math.toRadians(angleInRadians))*distanceFromArm + muzzleX// mussleX without the distance from the arm. I mean, the position directly on the shoulder of the Player should be mussleXdoublebulletY = Math.sin(Math.toRadians(angleInRadians))*distanceFromArm + muzzleY// see the comment on top ;)

I hope this helps. If you get weird issues, try switching sin and cos for x and y. I'm not sure which one to use for x and y

I wouldn't only rotate the sprite. (That could lead to Collision problems...)

I'd do it with trigonometry. Heard from sinus and cosinus?

Use them here

You could set other starting positions for the bullet:

1 2

doublebulletX = Math.cos(Math.toRadians(angleInRadians))*distanceFromArm + muzzleX// mussleX without the distance from the arm. I mean, the position directly on the shoulder of the Player should be mussleXdoublebulletY = Math.sin(Math.toRadians(angleInRadians))*distanceFromArm + muzzleY// see the comment on top ;)

I hope this helps. If you get weird issues, try switching sin and cos for x and y. I'm not sure which one to use for x and y

You are rotating around the spawn point. You want to rotate around the same location as you rotate the arm.

Yeah, but that's only because I did half the work so far. I only did the code to rotate the projectile-images, and that's all happening by getting the angle from the spawnpoint to the mouse, and it is the spawnpoint I need to rotate around the shoulder.

So, I just woke up and I think I got it right in my dream The spawnpoint is calculated from playerPosition, with an offset relative to that. So if I rotate the playerPosition point around the shoulder, and then add the offset x and y (multiplied by the sin and cos for the angle respectively), that should get me the right point, right?

I'm going to play around with it some more.

Thanks for all the suggestions, guys! I'll post back if I get it right

YAY! That wasn't so hard! I just had to remember my trigonometry. Man, I'd wish I had known I wanted to be a programmer when I was attending school. Then I might have listened to my SOB teachers, heheh.

Is it bad to make this amount of new variables every time a projectile is fired? Should I make all these variables global, since they're used all the time, so I don't put the GC on overdrive?The shoulder point should be a global final static, really.

This is working perfectly, for anyone who has the same question:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

publicvoidshootProjectile() {

// Currently fixed shoulder-offsets, from player-position (player-position is at his feet, in the middle)doubleshoulderX = player.getPosX(), shoulderY = player.getPosY()-40;

// Calculate the radians for the angle the bullets should be rotateddoubleangleInRad = Math.atan2(mouseController.getMouseY()-shoulderY, mouseController.getMouseX()-shoulderX);

Is it bad to make this amount of new variables every time a projectile is fired? Should I make all these variables global, since they're used all the time, so I don't put the GC on overdrive?

The primitive doubles are no objects and do not ever meet any GC. They end up on the stack or in registers.And storing point objects in class variables does not ease the life of the GC if you replace them as frequently as in your actual code. But "shooting garbage" is most likely only a fraction of the whole game loop garbage anyway.A modern garbage collector takes care of that while still being half-asleep.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

Should I make all these variables global, since they're used all the time, so I don't put the GC on overdrive?

The Oracle JVM can create and destroy 100 million objects per second. You could write a game with thousands of objects and recreate all the objects every single frame with 1000 frames per second and the GC woudn't break a sweat. On Android however things look different. But since doubles are no objects anyway it doesn't matter here.

A good friend of mine once told me: "In Java, EVERYTHING is an object"[size=2pt]And then he killed the dog.[/size]

Not the primitives. The primitives are pass-by-value keywords. You can use their object forms, yes, but it's more common to use the primitives. So, not everything is an object. And I feel sorry for the dog.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org