www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Hopefully a simple question...

reply WhatMeWorry <kheaser gmail.com> writes:
I'm converting some C++ and glm code to D and gl3n.  And I'm 
stumped at the following line.


GLboolean CheckCollision(BallObject &one, GameObject &two) // 
AABB - Circle collision
{
     // Get center point circle first
     glm::vec2 center(one.Position + one.Radius);
     . . .
}

one.Position is a vec2 (x and y) whereas Radius is a GLfloat

// my D code statement

vec2 center2 = one.position + one.radius;   // D code with gl3n

returns Error: incompatible types for ((one.position) + 
(one.radius)): 'Vector!(float, 2)' and 'float'

I agree with the D code. A vec and scalar can't be added 
together.  So why (or how) is the glm code working?


I looked at https://github.com/g-truc/glm project source, but I'm 
not even sure where to start?

Thanks
Jan 13
next sibling parent kinke <noone nowhere.com> writes:
On Friday, 13 January 2017 at 16:56:43 UTC, WhatMeWorry wrote:
 A vec and scalar can't be added together.  So why (or how) is 
 the glm code working?
The C++ source disagrees: https://github.com/g-truc/glm/blob/master/glm/detail/type_vec2.hpp#L219 It works via operator overloading, and adding a scalar simply means adding it to all vector components. In your case, you may get away by simply using the types in `core.simd`, such as `float2` instead of your `Vector` template. Otherwise, D operator overloading is way more elegant in D than in C++, see https://dlang.org/spec/operatoroverloading.html.
Jan 13
prev sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 13 January 2017 at 16:56:43 UTC, WhatMeWorry wrote:
 I'm converting some C++ and glm code to D and gl3n.  And I'm 
 stumped at the following line.


 GLboolean CheckCollision(BallObject &one, GameObject &two) // 
 AABB - Circle collision
 {
     // Get center point circle first
     glm::vec2 center(one.Position + one.Radius);
     . . .
 }

 one.Position is a vec2 (x and y) whereas Radius is a GLfloat

 // my D code statement

 vec2 center2 = one.position + one.radius;   // D code with gl3n

 returns Error: incompatible types for ((one.position) + 
 (one.radius)): 'Vector!(float, 2)' and 'float'

 I agree with the D code. A vec and scalar can't be added 
 together.  So why (or how) is the glm code working?


 I looked at https://github.com/g-truc/glm project source, but 
 I'm not even sure where to start?

 Thanks
If you're doing game dev stuff I'd highly recommend using/taking a look at https://github.com/d-gamedev-team/ and in particular using https://github.com/d-gamedev-team/gfm/ as your math library. It appears to support what you want https://github.com/d-gamedev-team/gfm/blob/master/math/gfm/math/vector.d#L53-59
Jan 13