How does one attach a radar to a battleship?

Official forum for the Chipmunk2D Physics Library.
Post Reply
metrix
Posts: 4
Joined: Fri Jul 01, 2011 7:37 am
Contact:

How does one attach a radar to a battleship?

Post 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.
aisman
Posts: 145
Joined: Tue Mar 04, 2008 2:21 am
Contact:

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

Post by aisman »

Why you need the pin?
Chipmunk4PB: The fastest way to write games together with PureBasic and the Chipmunk physics engine.
metrix
Posts: 4
Joined: Fri Jul 01, 2011 7:37 am
Contact:

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

Post by metrix »

The pin was to keep the 2 bodies moving in sync.. so the ships body could move the radars body
aisman
Posts: 145
Joined: Tue Mar 04, 2008 2:21 am
Contact:

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

Post 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
Chipmunk4PB: The fastest way to write games together with PureBasic and the Chipmunk physics engine.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

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

Post 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?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests