c++ - Box2d - Problems calculating impulse with speed and angle -


i have weapon bounces next enemy when hits.

i first begin calculating delta , getting angle:

float deltax = e->m_body->getposition().x - m_body->getposition().x; float deltay = e->m_body->getposition().y - m_body->getposition().y;  float angle = atan2((deltay), deltax) * 180 / m_pi; 

then convert angle vector , multiply 15 (the speed of projectile):

b2vec2 vec = b2vec2(cos(angle*m_pi/180),sin(angle*m_pi/180)); vec *= 15.0f; 

finally, apply impulse body:

m_body->applylinearimpulse(vec, m_body->getposition()); 

the problem vector must incorrect bullet not go in right direction. if output angle next enemy, tends output angle looks correct problem must in conversion vector.

i don't think need use trigonometry functions here, because have direction:

b2vec2 direction = e->m_body->getposition() - m_body->getposition(); direction.normalize(); // vector has length 1  float speed = ...; m_body->applylinearimpulse( speed * direction, m_body->getworldcenter() ); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -