Page 1 of 1

How does one attach a radar to a battleship?

Posted: Tue Jul 05, 2011 3:22 am
by metrix
So here is my difficulty.

I have a battle ship moving on a sea. I would like to control this ship by manipulating the forces on the ships body/shape.

Next i would like to attach another shape that rotates at a constant speed around the ship "sweeping" for other ships like a radar. I am thinking 360 degree rotation every 3 - 5 seconds (depending on power ups ;) )

I have tried adding a second body with a rectangle shape set as a sensor=true, pin it to the ships body and manually rotate the radar body in a scheduled event, but the system becomes unstable and throws the radar away after 30 seconds or there about.

I be stumped and am looking for inspiration as to a solution.

Re: How does one attach a radar to a battleship?

Posted: Tue Jul 05, 2011 6:25 am
by aisman
Why you need the pin?

Re: How does one attach a radar to a battleship?

Posted: Tue Jul 05, 2011 7:33 am
by metrix
The pin was to keep the 2 bodies moving in sync.. so the ships body could move the radars body

Re: How does one attach a radar to a battleship?

Posted: Tue Jul 05, 2011 7:55 am
by aisman
metrix wrote:The pin was to keep the 2 bodies moving in sync.. so the ships body could move the radars body
Seems to be a littly overhead.
You need only the current position of the ship.
Than you can calculate the current position of your radar point.
and now you can add the radar point to the space -> for collisions check.

A pin is not realy needed

Re: How does one attach a radar to a battleship?

Posted: Tue Jul 05, 2011 10:53 am
by slembcke
So you are using a big sensor shape and the collision callbacks? While that would work, it's sort of awkward and overkill. I'd just use a for loop over all the ship locations and do something like this:

Code: Select all

// current direction the radar is pointing in
cpVect radarDir = cpvforangle(radarDirRadians);

//wideness of the radar
cpFloat widenessCos = cos(radarWidenessRadians);

for each ship {
  cpVect shipDir = cpvnormalize(cpvsub(shipPosition, radarPosition));
  if(cpvdot(shipDir, radarDir) < widenessCos){
    // ship is in the radar field, check the distance maybe
  }
}
As for your original question, I'm not sure what the problem is. Can you post some code maybe?