Author Topic: Precise positioning of bullet sprite  (Read 4300 times)

Wyzemann

  • Regular
  • **
  • Posts: 26
    • View Profile
Precise positioning of bullet sprite
« on: 2011-03-29, 08:24:50 PM »
I could not find any documentation on how to solve this problem. The player shoots a "bullet", but it appears in the wrong place from the sprite. It should appear in front of the barrel of the gun but appears too low. It also does not appear the same distance from the player's center if facing left or right. However I have been careful to adjust the sprite's offset have a very precise collision rectangle.

My strategy was to create two framesets, one for left, and one for right so that it would appear in the precise location. I tried the different "addspritehere" relative positions but didn't find one that worked.

I went into the frameset and changed the bullet sprite's offset so that appears in the right place upon shooting, but now it does not match up to the sprite's collision.

I don't see an obvious or documented solution on the forums or the help file. Please advise.

Chris.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Precise positioning of bullet sprite
« Reply #1 on: 2011-03-30, 04:59:40 AM »
There are two things you could do.
1. You could use TestCollisionMask instead of TestCollisionRect.  Then the actual part of the sprite with non-transparent pixels will collide instead of the whole solidity rectangle of the sprite.
2. You could reset the bullet offset to 0 and fine tune the position of the sprite after it is created in one of two ways:
   a. Use SelectLastCreatedSprite and SetTargetParameter to change the "y" position of the sprite to be a specific vertical offset from the player sprites "y" value.  ParameterName would be "y" including quotes, and Value would by y + 18 if you want the top of the bullet to be 18 pixels below the top of the player sprite.
   b. Create a parameter on the bullet sprite to determine if it has been initialized yet.  For example, create a parameter named "Initialized".  If Initialized is zero, then use the "+" or "-" function to add or subtract some constant from the sprite's "y" value to adjust its position vertically immediately after it is created.  Then set Initialized to 1 with the "=" function so it doesn't keep adjusting.

Wyzemann

  • Regular
  • **
  • Posts: 26
    • View Profile
Re: Precise positioning of bullet sprite
« Reply #2 on: 2011-03-30, 08:41:41 AM »
The first solution will not fully solve the problem because I am testing it against reacting to solid tiles. However this gives me ideas once I have other sprites on the board.

The second solution is not working. I keep getting the error "cannot convert double to int" whenever the variable y is used.

here's the code:

this.SetTargetParameter("y", (18 + y));


Without the y it will compile. Not sure how to fix this.

Chris

Wyzemann

  • Regular
  • **
  • Posts: 26
    • View Profile
Re: Precise positioning of bullet sprite - Solved
« Reply #3 on: 2011-03-30, 01:15:55 PM »
I discovered that the PixelX and PixelY properties return an integer. Using this will fix the aforementioned problem.

this.SelectLastCreatedSprite();
               // SetLSWRelativePosition
               this.SetTargetParameter("y", (PixelY + 23));
               this.SetTargetParameter("x", (PixelX - 23));

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Precise positioning of bullet sprite
« Reply #4 on: 2011-03-31, 05:26:52 AM »
Just as a point of curiosity, how is the error reporting working out for you these days?  Did the error show a nice line with an error icon and a shortcut that took you directly to the rule causing the error?  (Are you using 2.1.9?)

Wyzemann

  • Regular
  • **
  • Posts: 26
    • View Profile
Error Reporting
« Reply #5 on: 2011-03-31, 09:28:06 AM »
The error reporting seems to be working fine. If I run into any failures of the error reporting I'll let you know. In this case it directed me to the source code since I have made it a function.


Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Precise positioning of bullet sprite
« Reply #6 on: 2011-05-07, 01:24:13 PM »
Another spambot it would seem?
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Precise positioning of bullet sprite
« Reply #7 on: 2011-05-09, 06:27:21 AM »
Yes, I deleted the account and all its posts.